node.js 如何正确关闭 socket.io / websocket-client?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5965733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:10:00  来源:igfitidea点击:

How does one properly shutdown socket.io / websocket-client?

node.jssocket.io

提问by Chris

I'm trying to create a test using LearnBoost's socket.ioand the node-websocket-client. Communication between the client and server work great. After all communication is done, I close both the client and the server. Yet the program hangs, waiting on some unknown callback. Two questions:

我正在尝试使用LearnBoost 的 socket.ionode-websocket-client创建一个测试。客户端和服务器之间的通信工作得很好。完成所有通信后,我关闭客户端和服务器。然而程序挂起,等待一些未知的回调。两个问题:

  1. What is the following program waiting for?
  2. Is there a tool for diagnosing outstanding callbacks in node programs?
  1. 下面的程序在等什么?
  2. 是否有用于诊断节点程序中未完成回调的工具?


var connect = require('connect'),
    io = require('socket.io'),
    WebSocket = require('websocket-client').WebSocket;

var port = 7111;

var server = connect.createServer();

var socket = io.listen(server); 
socket.on('connection', function(client) {
  client.send('Welcome!');

  client.on('message', function(message) {
    console.log(message);
  });

  client.on('disconnect', function() {
    console.log('closing');
    server.close();
  });
});

server.listen(port, function() {
  var ws = new WebSocket('ws://localhost:' + port + '/socket.io/websocket');
  ws.onmessage = function(message) {
    console.log(message.data);
  };

  setTimeout(function() {
    ws.send('~m~3~m~Yo!');
    ws.close();
  }, 10);
});

EDIT: changed the variable name of the WebSocket to wsto avoid confusion

编辑:更改了 WebSocket 的变量名称ws以避免混淆

回答by Raynos

var socket = io.listen(server);

You've created a socket on a port. You've never closed it.

您已经在端口上创建了一个套接字。你从来没有关闭它。

socket.server.close()closes your (socket.io) socket.

socket.server.close()关闭您的 (socket.io) 套接字。

When in doubt read the socket.iogithub examples

如有疑问,请阅读socket.iogithub示例

socket.server === serverIt's the server you pass in, in the liste statement so it's closed. I'm not sure what it's waiting for.

socket.server === server这是您在 liste 语句中传入的服务器,因此它已关闭。我不确定它在等什么。

回答by ppcano

Below a way to shutdown all the connections and be able to run multiple expresso tests (using socket.io and socket.io-client).

下面是关闭所有连接并能够运行多个 expresso 测试的方法(使用 socket.io 和 socket.io-client)。

The solution is tricky and buggy but works on 0.8.5. The main problem is regarding the library to use websockets (node-websocket-client).

该解决方案是棘手和错误的,但适用于 0.8.5。主要问题是关于使用 websockets (node-websocket-client) 的库。

Currently, on socket.io, the OS contributors have patched the websocket client. So, we must do the same on our socket.io-client npm package to be able to use finishClose method on the socket client side. Socket.io-client uses the websocket library as npm package, so you must find the file (websocket.js) and substitute it with the same on socket.io.

目前,在 socket.io 上,操作系统贡献者已经修补了websocket 客户端。所以,我们必须在 socket.io-client npm 包上做同样的事情,才能在 socket 客户端使用 finishClose 方法。Socket.io-client 使用 websocket 库作为 npm 包,因此您必须找到文件(websocket.js)并将其替换为 socket.io 上的相同文件。

Afterwards, you could use finishClose method to ensure the connections are closed and with some custom server/client socket settings, the tests will run correctly.

之后,您可以使用 finishClose 方法来确保连接关闭,并且使用一些自定义服务器/客户端套接字设置,测试将正确运行。

  var  io = require("socket.io").listen(port);

  io.set('close timeout', .2);
  io.set('client store expiration', .2);

  var client = require("socket.io-client").connect( "http://localhost", { port: port ,  'reconnect': false, 'force new connection': true});

  client.on('connect', function() {

        client.disconnect();

  });

  client.on('disconnect', function() {

      client.socket.transport.websocket.finishClose();
      io.server.close();

  });

  io.server.on('close', function() {

      setTimeout( function() {

        done();

      }, 500);
  });

Hope, somebody can help.

希望,有人可以帮忙。

回答by Detect

The program is waiting because socket.io (server) is still listening for incoming connections. I don't know of any way to stop listening.

该程序正在等待,因为 socket.io(服务器)仍在侦听传入连接。我不知道有什么办法可以停止聆听。