unity前端部分
socketIO.On("connect", (e) => { Debug.Log("连接成功"); });
socketIO.On("connecting", (e) => { Debug.Log("正在连接"); });
socketIO.On("disconnect", (e) => { Debug.Log("断开连接"); });
socketIO.On("connect_failed", (e) => { Debug.Log("连接失败"); });
socketIO.On("error", (e) => { Debug.Log("错误发生,并且无法被其他事件类型所处理"); });
socketIO.On("message", (e) => { Debug.Log("同服务器端message事件"); });
socketIO.On("anything", (e) => { Debug.Log("同服务器端anything事件"); });
socketIO.On("reconnect_failed", (e) => { Debug.Log("重连失败"); });
socketIO.On("reconnect", (e) => { Debug.Log("成功重连"); });
socketIO.On("reconnecting", (e) => { Debug.Log("正在重连"); });
Dictionary<string, string> data = new Dictionary<string, string>();
data["username"] = "liluo";
data["password"] = "000";
socketIO.Emit("login", new JSONObject(data));
socketIO.On("login", (e) =>
{
Debug.Log(e.data);
string jsonTest = e.data.ToString();
ModelTest obj = JsonUtility.FromJson<ModelTest>(jsonTest);
Debug.Log(obj.statu);
Debug.Log(obj.number);
foreach (int inter in obj.list)
{
Debug.Log(inter);
}
});
[System.Serializable]
public class ModelTest
{
public string statu;
public int number;
public int[] list;
}
string name = inputName.GetComponent<TMP_Text>().text;
name = name.Substring(0, name.Length - 1);
node.js后端部分
var io = require('socket.io')(8001);
io.on('connection', function (socket) { console.log('用户连接');
socket.on('show', function(msg) {
socket.join('game');
socket.leave('game');
io.emit("给所有人");
socket.emit("给链连接者");
socket.to(msg).emit('hey', 'I just met you');
socket.broadcast.emit('给除去链接者');
socket.to('game').emit("给除去连接者该房间的所有人");
socket.in('game').emit("给该房间的所有人");
});
socket.on('disconnect', function () { console.log('用户登出'); });
});