简体   繁体   English

发出特定客户端NODE javascript后socket.io断开连接

[英]socket.io disconnect after emitting to specific client NODE javascript

Hello I am currently working on some kind of "Chat"-system. 您好,我目前正在开发某种“聊天”系统。

Server: NodeJs with express - https Communication: Socket.io@1.0 服务器:带有Express的NodeJ-https通信:Socket.io@1.0

client: 客户:

var socket = io.connect();
socket.emit('HELLO', {user:'someuserid'});
socket.on('WELCOME', function(data){
    socket.emit('REGISTER', {});
});
socket.on('SIGNED', function(data){
    console.log(data.user);
    socket.emit('GO', {});
});
socket.on('message', function(data){
    console.log(data.somedata);
});

server: 服务器:

io.sockets.on('connection', function(socket){
    socket.on('HELLO', function(data){
        socket.user = data.user;
        socket.emit('WELCOME', socket.user);
    });
    socket.on('REGISTER', function(data){
        console.log('room: '+socket.user);
        socket.join(socket.user);
        socket.emit('SIGNED', socket.user);
        console.log('SIGNED IN: '+socket.user);

    });
    socket.on('GO', function(data){
        //some logic happens where a list of users gets loaded
        //which is identical to the socket.user and its room
        //... loop
        io.in(partners[i].user).emit('message', { somedata: 'asd' });
    }

    socket.on('disconnect' ..... blaaa blaa)

So, basicly what I tried to do here is create a workaround to sending a message to a specific user by sending a message to a specific room. 因此,基本上,我在这里尝试做的是创建一种变通方法,通过将消息发送到特定房间来向特定用户发送消息。

this: 这个:

 io.in(partners[i].user).emit('message', { somedata: 'asd' });

and this: 和这个:

 partners[i].socket.emit('message', { somedata: 'asd' });

result in the same: 结果相同:

room: 540246a3e4b0a64a28e1ec59
SIGNED IN: 540246a3e4b0a64a28e1ec59
room: 540504ba0b526b274398480e
SIGNED IN: 540504ba0b526b274398480e
to: 540246a3e4b0a64a28e1ec59
disconnected:540246a3e4b0a64a28e1ec59

the user registers, gets connected and wants to emit a message to specific chatpartners in the array partners[i]. 用户注册,建立连接并希望向阵列合作伙伴[i]中的特定chatpartners发送消息。

once the emit is fired the user, the message is supposed to be emitted to disconnects... 一旦发射了用户,就应该发送消息以断开连接...

what am I doing wrong? 我究竟做错了什么?

(the script is obviously not complete.. i pasted the most important parts) (脚本显然不完整。。我粘贴了最重要的部分)

Thanks for your help. 谢谢你的帮助。

I think I have found the solution: 我想我已经找到了解决方案:

sometimes using google is more effort than debugging by yourself. 有时使用Google比自己调试更费力。

After scrolling through the debug log in firefox I found out that this problem actually had to do with the code inside my socket 'message' handler. 在firefox中浏览调试日志后,我发现此问题实际上与套接字“消息”处理程序中的代码有关。 A snippet that might help others to find errors with their sockets: 可以帮助其他人找到其套接字错误的代码段:

socket.on('error', function (err) {
  if (err.description) throw err.description;
  else throw err; // Or whatever you want to do
});

I think this is an issue in socket.io although it was my wrong code - the socket.io errorhandler should have passed that through. 我认为这是socket.io中的问题,尽管这是我的错误代码-socket.io错误处理程序应该已经通过了。

Have fun 玩得开心

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM