Node.js:socket.io关闭客户端连接 [英] Node.js: socket.io close client connection

查看:652
本文介绍了Node.js:socket.io关闭客户端连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用:




  • socket.io 0.9

  • node.js 0.10.15

  • express 3.3.4



ie:
调用 localhost / test

- 服务器端

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

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

socket.on('disconnect',function(){
console.log('disconnect event');
//socket.manager.onClientDisconnect(socket.id); - >无休止的连接事件在服务器端
//socket.disconnect( ); - > same here
});
});

- 客户端

  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
});

如果我加载测试页面,我需要一些值从服务器(getInitData)。

在第一页访问我得到的数据一次,重新加载或第二次访问我得到它两次等等。



服务器端的连接在页面重新加载时自动关闭,如果您离开页面。

但是在客户端,连接是仍然打开。

如何关闭客户端的连接或检查是否已经有开放的连接?



更新

我现在尝试了以下:(客户端)

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

这在客户端触发了断开连接事件,但我仍然收到两次或三次响应。 / p>

解决方案

在服务器端和/或浏览器端没有连接。只有一个连接。如果其中一方关闭它,那么它将被关闭(并且您无法将数据推送到明显关闭的连接)。



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



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


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

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

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.

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.

解决方案

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-alive but hopefuly socket.io handles this correctly).

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.

这篇关于Node.js:socket.io关闭客户端连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆