我有一个奇怪的问题。我正在编写一个简单的Socket.IO echo服务器作为测试,但我无法使send()方法正常工作。在服务器上我使用这段代码:Socket.IO send()不工作
var io = require('socket.io').listen(3000);
var clients = {};
io.sockets.on('connection', function (socket) {
socket.send('test');
socket.on('addclient', function(id) {
socket.id = id;
clients[id] = socket;
console.log('New client connected: '+ id);
});
socket.on('echomessage', function(message) {
console.log('Sending echo message to client '+ socket.id);
socket.send(message);
});
socket.on('disconnect', function() {
console.log('Client '+ socket.id + ' disconnected');
delete clients[socket.id];
});
});
的发送(“测试”)是可以正常使用,但是当我发出echomessage事件,我没有收到该消息。服务器或客户端没有任何错误消息。
所以,在客户方我这样做:
// Connect with the server (works, connection established)
// works too, I see it on the server
sock.emit('addclient', 1);
// I see 'Sending echo message to client 1' on the server
sock.emit('echomessage', 'Echo this please');
但我完全不接收消息。
我完全不知道我做错了什么。所有帮助表示赞赏!
检查,如果他的客户端连接到网络插座,他只能放弃味精。 – 2012-04-26 15:18:07
客户端上的哪些代码处理服务器发送的消息? – supertopi 2012-04-26 18:10:36
我在客户端上使用Java实现。客户端已连接,因为我从来没有在客户端或服务器端获得“连接终止”消息。它似乎只是socket.send(消息);当echomessage事件被发射时,一块不起作用。 – EsTeGe 2012-04-29 12:26:40