A realtime chat code based on C# client with Node.js Server, supports multi-chatroom & private individual chats
中文请点这里
Note: This is a setup example on CentOS Server
Install Node.js
$ cd / # go to root dictionary
$ wget https://nodejs.org/dist/latest-v9.x/node-v9.4.0-linux-x64.tar.xz # download node.js v9 from official siteDecompress Node.js
$ tar -xvJf node-v9.4.0-linux-x64.tar.xzMove it to /usr/local Dictionary
$ mv node-v9.4.0-linux-x64 /usr/local/node-v9Create Symbolic Links
$ ln -s /usr/local/node-v9/bin/node /bin/nodeSetup NPM
$ ln -s /usr/local/node-v9/bin/npm /bin/npmSetup environment variable
$ echo 'export PATH=/usr/local/node-v9/bin:$PATH' >> /etc/profile
$ source /etc/profileUse NPM
$ npm install forever -gInstall socket.io
$ npm install socket.ioClone this project
$ git clone https://github.com/JasonXuDeveloper/Realtime-Socket-Chat.gitGo to Server dictionary
$ cd ServerStart the Server (make sure you have access to port 4567)
$ node main.js
Server start....Server side setup is now DONE!!!
Note: Client setup fits all operating systems which can install Unity3d (or you build project to different platforms via Unity)
- Press Here to go to our Github page (ignore if you are already here)
- Download ZIP
- Decompress ZIP
- Go to Unity-DEMO/Asstes/Scenes
- Double click file: "DEMO SCENE.unity"
- On hierarchy, select SocketIO, and on inspector, change ws://127.0.0.1:4567/socket.io/?EIO=4&transport=websocket to you ip address (ws://your_ip:4567/socket.io/?EIO=4&transport=websocket)
- Run it and enjoy your chatroom!
Core class which sends messages and does server-side actions
//This is the base class on c# which is the core code
[Serializable]
public class Client
{
//Use this method to set a new ID (Name) for a client
public void SetID(string newID)
{
//xxx;
//xxx;
}
//Login to server so that the client can send messages
public void Login()
{
//xxx;
//xxx;
}
//Use this method to send message
public async void SendMessage(string msg)
{
//xxx
//xxx
}
}
/*
* Run server code first
* Then, try this code (or try the unity example)
*/
public class Main
{
void Main()
{
var Client = new Client();
Client.SetID("Test");
Client.SendMessage("Hi There!");
//See what will happen on the server
}
}This is the main UI Setup & Server listener code. See Unity-DEMO/Scripts/Chatroom.cs for details.
//What we have is the most imortant piece of code here:
Socket.On("recieveMsg",(e)=>
{
var id = e.data.list[0].str;
var text = e.data.list[1].str;
//Codes here for showing what we recieved;
});You can check the history.txt file in Server dictionary, which is the chat history overall
var server=require('socket.io');//socket
var fs=require('fs');//file stream
//saves chat history
fs.access('history.txt',(err)=>{
if(err){
fs.writeFile('history.txt','','utf8',function(error){
if(error){
console.log(error);
return false;
}
console.log('Successfully created history.txt');
});
}
});
var io = new server(4567);//you can change 4567 here to port you want
console.log('Server Start....');
var ids = [];//a list which saves current ids to avoid same names
//a function which makes unique id
function guid() {
return xxx;
}
//when socker connected
io.on('connection', function(socket){
//xxx;
}C# based client source code (running on Unity 3d)Node.js based server source code (running on Server, could be Virtual Machine, Node.js supports Windows, MaxOS and all kind of Linux)Unity3d DEMO PROJECT- Chat between individuals
If you have any issues, please follow jason_the_developer at instagram and send me messages!
基于C#客户端以及Node.js服务端的聊天室,支持多人聊天室以及私人聊天
注意:该服务端部署仅适用于CentOS机器
下载Node.js
$ cd / # 切换到根目录
$ wget https://nodejs.org/dist/latest-v9.x/node-v9.4.0-linux-x64.tar.xz # 从官网下载解压Node.js
$ tar -xvJf node-v9.4.0-linux-x64.tar.xz移动解压文件到/usr/local Dictionary
$ mv node-v9.4.0-linux-x64 /usr/local/node-v9创建软链接
$ ln -s /usr/local/node-v9/bin/node /bin/node配置NPM
$ ln -s /usr/local/node-v9/bin/npm /bin/npm配置环境变量
$ echo 'export PATH=/usr/local/node-v9/bin:$PATH' >> /etc/profile
$ source /etc/profile使用NPM
$ npm install forever -g下载socket.io
$ npm install socket.io克隆该项目
$ git clone https://github.com/JasonXuDeveloper/Realtime-Socket-Chat.git进入服务端目录
$ cd Server启动服务器(确保端口4567可用)
$ node main.js
Server start....服务端配置完成!!!
注意:客户端可以在任何可以使用Unity3d的操作系统运行(或者通过Unity打包项目到其他平台)
- 点击这里进入项目Github地址(如果你已经在这个页面请忽略)
- 下载项目ZIP
- 解压ZIP
- 进入目录Unity-DEMO/Asstes/Scenes
- 双击文件: "DEMO SCENE.unity"
- 在阶层中,选择SocketIO,同时,在检查器中,将ws://127.0.0.1:4567/socket.io/?EIO=4&transport=websocket改为你的ip地址(ws://your_ip:4567/socket.io/?EIO=4&transport=websocket)
- 运行并尽情测试你的聊天室是否可用!
处理服务器回调以及发送信息的核心代码
//This is the base class on c# which is the core code
[Serializable]
public class Client
{
//Use this method to set a new ID (Name) for a client
public void SetID(string newID)
{
//xxx;
//xxx;
}
//Login to server so that the client can send messages
public void Login()
{
//xxx;
//xxx;
}
//Use this method to send message
public async void SendMessage(string msg)
{
//xxx
//xxx
}
}
/*
* Run server code first
* Then, try this code (or try the unity example)
*/
public class Main
{
void Main()
{
var Client = new Client();
Client.SetID("Test");
Client.SendMessage("Hi There!");
//See what will happen on the server
}
}展示UI以及接收服务器信息的核心代码 查看Unity-DEMO/Scripts/Chatroom.cs以获得更多详情
//What we have is the most imortant piece of code here:
Socket.On("recieveMsg",(e)=>
{
var id = e.data.list[0].str;
var text = e.data.list[1].str;
//Codes here for showing what we recieved;
});可以在Server目录下History.txt查阅完整全部客户聊天记录
var server=require('socket.io');//socket
var fs=require('fs');//file stream
//saves chat history
fs.access('history.txt',(err)=>{
if(err){
fs.writeFile('history.txt','','utf8',function(error){
if(error){
console.log(error);
return false;
}
console.log('Successfully created history.txt');
});
}
});
var io = new server(4567);//you can change 4567 here to port you want
console.log('Server Start....');
var ids = [];//a list which saves current ids to avoid same names
//a function which makes unique id
function guid() {
return xxx;
}
//when socker connected
io.on('connection', function(socket){
//xxx;
}基于C#的Unity3d客户端Node.js服务端源代码Unity3d实例项目- 私聊解决方案
有问题请加作者QQ:2313551611, 并标明是从Github来反馈问题的!