socket.io 客户端未接收
我在nodejs 中使用socket.io,并且能够将数据从客户端发送到服务器。但是当我从服务器发出时,客户端似乎没有收到这个...... 我缺少什么?
服务器:
socket = io.listen(app);
socket.sockets.on('connection', function(client){
client.on('something-from-client', function(msg){
console.log(msg);
//do something.
client.emit('some-result',{"total":docs.length});
});
});
客户端:
var socket = io.connect('http://localhost:9999');
socket.on('some-result', function(data){
console.log('received from server', data);
});
socket.emit("something-from-client", {"lat":lat, "lng":lng});
I am using socket.io in nodejs and I am able to send data from client to server. But when I emit from server, the client does not seem to be receiving this...
What am i missing ?
server:
socket = io.listen(app);
socket.sockets.on('connection', function(client){
client.on('something-from-client', function(msg){
console.log(msg);
//do something.
client.emit('some-result',{"total":docs.length});
});
});
client:
var socket = io.connect('http://localhost:9999');
socket.on('some-result', function(data){
console.log('received from server', data);
});
socket.emit("something-from-client", {"lat":lat, "lng":lng});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,它可以工作了,如果正在使用 Express,则需要在服务器端进行一些小的更改
正如所承诺的 - 工作代码 - https://github.com/parj/node-websocket-demo/tree/socket_emit
CF 参考 - http://socket.io/#how-to-use
Ok got it working, if express is being used, some minor changes need to be done on server side
As promised - Working code - https://github.com/parj/node-websocket-demo/tree/socket_emit
CF Reference - http://socket.io/#how-to-use