use*_*032 6 sockets unity-game-engine node.js
我试图使用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
}
Run Code Online (Sandbox Code Playgroud)
....
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);
});
});
Run Code Online (Sandbox Code Playgroud)
您的活动可能以错误的顺序附加,请尝试如下:
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();
}
Run Code Online (Sandbox Code Playgroud)
对于节点:
io.sockets.on('connection', function(socket) {
console.log('client connected');
socket.emit('message', 'Hello World!');
});
Run Code Online (Sandbox Code Playgroud)
也不允许客户决定自己的 ID,因为在大多数情况下它是“可破解的”。只有服务器才应该做出重要的决定,而不是客户端。
| 归档时间: |
|
| 查看次数: |
11698 次 |
| 最近记录: |