Node.js:socket.io 关闭客户端连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18126677/
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 15:14:32  来源:igfitidea点击:

Node.js: socket.io close client connection

javascriptnode.jsexpresssocket.io

提问by baam

How can I close the socket connection on the client side?

如何关闭客户端的套接字连接?

I am using:

我在用:

  • socket.io 0.9
  • node.js 0.10.15
  • express 3.3.4
  • socket.io 0.9
  • 节点.js 0.10.15
  • 快递 3.3.4

i.e.: call localhost/test
-- server side

即:调用localhost/test
——服务器端

var test = io
.of('/test')
.on('connection', function (socket) {

  console.log('open socket: ' + socket);

  socket.on('disconnect', function () {
    console.log('disconnected event');
    //socket.manager.onClientDisconnect(socket.id); --> endless loop with this disconnect event on server side
    //socket.disconnect(); --> same here
  });
});

-- client side

- 客户端

var socket = io.connect('http://localhost:3000/test');
socket.on('disconnect', function () {
   console.log('disconnect client event....');
});

socket.emit('getInitData', function (data) {
  .. do something with data
});

If I load the test-page I need some values from the server (getInitData).
On the first page visit I get the data once, on a reload or second visit I get it twice and so on.

如果我加载测试页面,我需要来自服务器的一些值 (getInitData)。
在第一次访问页面时,我获得了一次数据,在重新加载或第二次访问时,我获得了两次数据,依此类推。

The connection on the server side is beeing closed automatically on page reload and if you leave the page.
But on the client side the connection is still open.
How can I close the connection on the client side or check if there is already a open connection?

服务器端的连接在页面重新加载和离开页面时自动关闭。
但是在客户端,连接仍然打开。
如何关闭客户端的连接或检查是否已经有打开的连接?

UPDATE
I tried now the following: (client side)

更新
我现在尝试了以下内容:(客户端)

window.onbeforeunload = function(e) {
  socket.disconnect();
};

This triggers on the client side the disconnect event, but I still get the twice or tripple response.

这会在客户端触发断开连接事件,但我仍然得到两次或三次响应。

采纳答案by freakish

There is no such thing as connection on server side and/or browser side. There is only one connection. If one of the sides closes it, then it is closed (and you cannot push data to a connection that is closed obviously).

服务器端和/或浏览器端没有连接之类的东西。只有一种联系。如果其中一侧关闭它,则它被关闭(并且您不能将数据推送到明显关闭的连接)。

Now a browser closes the connection when you leave the page (it does not depend on the library/language/OS you are using on the sever-side). This is at least true for WebSockets (it might not be true for long polling because of keep-alivebut hopefuly socket.io handles this correctly).

现在,当您离开页面时,浏览器会关闭连接(它不依赖于您在服务器端使用的库/语言/操作系统)。至少对于 WebSockets 来说是这样(对于长轮询可能不是这样,因为keep-alive但希望 socket.io 能够正确处理这个问题)。

If a problem like this happens, then I'm pretty sure that there's a bug in your own code (on the server side). Possibly you are stacking some event handlers where you should not.

如果发生这样的问题,那么我很确定您自己的代码中存在错误(在服务器端)。可能您正在堆叠一些不应该的事件处理程序。

回答by Milan Babu?kov

Did you try:

你试过了吗:

socket.disconnect() 

on client?

在客户端?

回答by Himani Agrawal

For socket.io version 1.4.5:

对于 socket.io 版本 1.4.5:

On server:

在服务器上:

socket.on('end', function (){
    socket.disconnect(0);
});

On client:

在客户端:

var io = io();
io.emit('end');

回答by Ali Azhar

socket.disconnect()

Only reboots the connection firing disconnect event on client side. But gets connected again.

仅在客户端重新启动连接触发断开事件。但是又连接上了。

socket.close()

Disconnect the connection from client. The client will keep trying to connect.

断开与客户端的连接。客户端将继续尝试连接。

回答by Ahmad Zahabi

socket.disconnect()is a synonym to socket.close()which disconnect the socket manually.

socket.disconnect()socket.close()手动断开套接字的同义词。

When you type in client side :

当您输入客户端时:

const socket = io('http://localhost');

this will open a connection with autoConnect: true, so the lib will try to reconnect again when you disconnect the socket from server, to disable the autoConnection:

这将打开与 的连接autoConnect: true,因此当您断开套接字与服务器的连接时,lib 将尝试再次重新连接,以禁用自动连接:

const socket = io('http://localhost', {autoConnect: false});
socket.open();// synonym to socket.connect()

And if you want you can manually reconnect:

如果你愿意,你可以手动重新连接:

socket.on('disconnect', () => {
  socket.open();
});

回答by JakubKnejzlik

I'm trying to close users connection in version 1.0 and found this method:

我试图在 1.0 版中关闭用户连接并找到了这个方法:

socket.conn.close()

The difference between this method and disconnect() is that the client will keep trying to reconnect to the server.

此方法与 disconnect() 的区别在于客户端将不断尝试重新连接到服务器。

回答by Luis Fletes

try this to close the connection:

试试这个来关闭连接:

socket.close();

and if you want to open it again:

如果你想再次打开它:

socket.connect();

回答by Lalit Kumar Maurya

Just try socket.disconnect(true)on the server side by emitting any event from the client side.

只需socket.disconnect(true)通过从客户端发出任何事件来尝试在服务器端。