Socket.io 指定心跳 [英] Socket.io specifying heartbeat

查看:110
本文介绍了Socket.io 指定心跳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎不知道如何为 socket.io 设置心跳选项?我希望能够指定当超时"发生时会发生什么,以及服务器在一段时间内没有收到客户端的任何响应时会发生什么.

I can't seem to find out how to set the heartbeat options for socket.io? I'd love to be able to specify what happens when a "timemout" happens, and when the server hasn't gotten any response from the client for some time.

由于某种原因,断开连接事件并不总是触发,即使它已经过去了一个多小时而没有连接到客户端.这经常发生在独立的 Java socket.io 客户端

For some reason the disconnect event isn't always firing, even though it has passed over an hour without the client not being connected. This often happens with the standalone Java socket.io client

推荐答案

Socket.IO 使用 engine.io,它允许你指定 ping 间隔和 ping 超时,像这样:

Socket.IO uses engine.io, which allows you to specify ping interval and ping timeout, like so:

var app = require('express')();
var http = require('http').Server(app);

var io = require('socket.io')(http, {'pingInterval': 2000, 'pingTimeout': 5000});

(2 秒间隔和 5 秒超时只是一个例子.对于您的用例来说可能很频繁)

(2 second interval and 5 second timeout is just an example. It might be to frequent for your use case)

这篇关于Socket.io 指定心跳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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