我正在尝试使用socket.io 将我的unity3d 程序与node.js 服务器连接。
使用UnitySocketIO,我成功地建立了客户端和服务器之间的连接。
但是,On 或 Emit 方法不起作用。
有人可以帮我解决这个问题吗?
void Start () {
string socketUrl = "http://127.0.0.1:50122";
Debug.Log("socket url: " + socketUrl);
this.socket = new Client(socketUrl);
this.socket.Opened += this.SocketOpened;
this.socket.Message += this.SocketMessage;
this.socket.SocketConnectionClosed += this.SocketConnectionClosed;
this.socket.Error += this.SocketError;
this.socket.Connect();
}
private void SocketOpened (object sender, EventArgs e) {
Debug.Log("socket opened"); // i got this message
this.socket.On ("message", (data) => {
Debug.Log ("message : " + data);
});
this.socket.Emit("join", "abc");
Debug.Log("Emit done"); // i got this message
}
....
io.sockets.on('connection', function (socket) {
console.log('connect'); // i got this message
socket.emit('message', 'Hello World!');
socket.on('join', function (id) {
console.log('client joined with id ' + id);
socket.emit('message', 'Hello ' + id);
});
});
您的活动可能以错误的顺序附加,请尝试如下:
void Start() {
string socketUrl = "http://127.0.0.1:50122";
Debug.Log("socket url: " + socketUrl);
this.socket = new Client(socketUrl);
this.socket.Opened += this.SocketOpened;
this.socket.Message += this.SocketMessage;
this.socket.SocketConnectionClosed += this.SocketConnectionClosed;
this.socket.Error += this.SocketError;
this.socket.On("message", (data) => {
Debug.Log("message: " + data);
});
this.socket.Connect();
}
对于节点:
io.sockets.on('connection', function(socket) {
console.log('client connected');
socket.emit('message', 'Hello World!');
});
也不允许客户决定自己的 ID,因为在大多数情况下它是“可破解的”。只有服务器才应该做出重要的决定,而不是客户端。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)