socket.io客户端连接无法在第二次进行

时间:2021-01-30 18:31:45

Currently, I am implementing an API using nodejs express, then it needs to connect to socket.io and send event.

目前,我正在使用nodejs express实现API,然后需要连接到socket.io并发送事件。

The API is located in socket.io-client (client), and it connects to socket.io (server)

API位于socket.io-client(客户端)中,它连接到socket.io(服务器)

1st API call: success

The connection is made for the 1st call of the API, message is sent and socket can be disconnected, with the 'disconnect' callback is invoked both on client and server side.

连接是为了第一次调用API,发送消息并且可以断开套接字,在客户端和服务器端都调用'disconnect'回调。

2nd API call: failure

When the API is invoked the 2nd time, the connection to server cannot be made, 'client' callback on client side is not called.

当第二次调用API时,无法建立与服务器的连接,不会调用客户端的“客户端”回调。

3rd API call: success

Then I tried to restart the client side, keeping other things unchanged. The API is called again, and the connection to socket.io is made successfully and everything is fine.

然后我尝试重新启动客户端,保持其他内容不变。再次调用API,并成功连接到socket.io,一切正常。

Can anyone explain the logistics behind this?

任何人都可以解释这背后的物流吗?

Updated

client.js

   App.getByUserId(message.to_id, function(error, app) {
  var  socket = io.connect('http://127.0.0.1:9002');
  socket.on('connect', function(){
  console.log("client connect socket id:" + socket.id);
    console.log("appkey:" + app.private_token);
    socket.emit('appkey.check',{appkey: app.private_token, uuid: message.to_id.uuid},    function(data){
        socket.emit("forceDisconnect");
        socket = null;

    });
});

2 个解决方案

#1


11  

You just hit one of Socket.IO's many "features" or "bugs" depending how you see this. Socket.IO tries to be smart and re-use connections (which causes a lot of connection issues actually) The way around this is use the force new connection option in your io.connect:

根据你的看法,你只需点击一个Socket.IO的许多“功能”或“错误”。 Socket.IO试图变得聪明并重新使用连接(实际上会导致很多连接问题)解决这个问题的方法是使用io.connect中的force new connection选项:

io.connect('http://127.0.0.1:9002', { 'force new connection': true });

io.connect('http://127.0.0.1:9002',{'强制新连接':true});

What you could also do is use https://github.com/primus/primus which wraps Socket.IO if you use the socket.io transformer. Internally, it completely removes the use of the io.connect and uses the much more lower level io.Socket constructor to create more stable connections that you would get with a stock socket.io.

您还可以使用https://github.com/primus/primus,如果您使用socket.io转换器,它将包装Socket.IO。在内部,它完全消除了io.connect的使用,并使用更低级别的io.Socket构造函数来创建更稳定的连接,这将通过stock socket.io获得。

#2


0  

With socket 1.0+, you have to use this for forcing new connection.

使用socket 1.0+,您必须使用它来强制新连接。

io.connect(SERVER_IP, { 'forceNew': true });

#1


11  

You just hit one of Socket.IO's many "features" or "bugs" depending how you see this. Socket.IO tries to be smart and re-use connections (which causes a lot of connection issues actually) The way around this is use the force new connection option in your io.connect:

根据你的看法,你只需点击一个Socket.IO的许多“功能”或“错误”。 Socket.IO试图变得聪明并重新使用连接(实际上会导致很多连接问题)解决这个问题的方法是使用io.connect中的force new connection选项:

io.connect('http://127.0.0.1:9002', { 'force new connection': true });

io.connect('http://127.0.0.1:9002',{'强制新连接':true});

What you could also do is use https://github.com/primus/primus which wraps Socket.IO if you use the socket.io transformer. Internally, it completely removes the use of the io.connect and uses the much more lower level io.Socket constructor to create more stable connections that you would get with a stock socket.io.

您还可以使用https://github.com/primus/primus,如果您使用socket.io转换器,它将包装Socket.IO。在内部,它完全消除了io.connect的使用,并使用更低级别的io.Socket构造函数来创建更稳定的连接,这将通过stock socket.io获得。

#2


0  

With socket 1.0+, you have to use this for forcing new connection.

使用socket 1.0+,您必须使用它来强制新连接。

io.connect(SERVER_IP, { 'forceNew': true });