Socket.io“连接"在每个客户端心跳上触发的事件? [英] Socket.io "connection" event fired on every client heartbeat?

查看:141
本文介绍了Socket.io“连接"在每个客户端心跳上触发的事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的基本 Socket.io 服务器设置:

I have a basic Socket.io server setup like this:

var server = express.createServer().listen(port);
    this.io = socketIO.listen(server);

this.io.sockets.on('connection', function(socket){
        initSocket(socket);
    });

我也将 socket.io 配置为使用 XHR 轮询,如下所示:

I also have socket.io configured to use XHR polling like so:

io.set("transports", ["xhr-polling"]);
io.set("close timeout", 3);
io.set("heartbeat interval", 2); 

问题是每次客户端发送心跳(每 2 秒)时,都会触发 'connection' 事件.有没有我可以使用的不同事件,每次套接字最初连接时都会触发一次?

The issue is every time the client sends a heartbeat (every 2 sec), the 'connection' event is being fired. Is there a different event I can use that will fire once each time a socket initially connects?

我会使用授权"事件,但这只会传入握手对象,而不是实际的套接字.

I would use the "authorization" event, but that only passes in a handshake object not the actual socket.

推荐答案

发现问题.我将我的 xhr轮询持续时间"设置为 heroku 建议的 10 秒,如下所示:

Found the problem. I had my xhr "polling duration" set to heroku's suggested 10s like so:

io.set("polling duration", 10);

这意味着客户端每 10 秒只发出一个新的 xhr 请求(只要前一个请求返回).同时,我将关闭超时"设置为 3 秒:

Which means that the client only makes a new xhr request every 10 seconds (as soon as the previous request returns). At the same time I had the "close timeout" set to 3 seconds:

io.set("close timeout", 3);

这意味着如果服务器在上次请求后 3 秒内没有收到客户端的消息,它会关闭连接,因此会触发连续的断开/连接"事件.

Which means if the server does not hear from the client within 3 seconds since its last request it closes the connection, hence the continuous 'disconnect/connection' events being fired.

解决方案是将关闭超时设置为高于轮询持续时间.心跳间隔"似乎与 xhr 连接无关.

The solution was to set the close timeout higher than the polling duration. It would seem that the "heartbeat interval" is not relevant for xhr connections.

这篇关于Socket.io“连接"在每个客户端心跳上触发的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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