Socket.io指定心跳

时间:2015-06-29 15:29:56

标签: javascript node.js socket.io

我似乎无法找到如何为socket.io设置心跳选项?我希望能够指定“timemout”发生时会发生什么,以及服务器在一段时间内没有得到客户端的任何响应。

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

2 个答案:

答案 0 :(得分:18)

Socket.IO使用engine.io,允许你指定ping interval and ping timeout,如下所示:

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

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

(2秒间隔和5秒超时只是一个例子。它可能会频繁用于您的用例)

答案 1 :(得分:9)

对我来说,以下工作:

int n;    
int main(void)
{
    int sum;
    char buffer[BUF_LEN];

    /* open character device   */
    int fd = open("/dev/summer0", O_RDWR);  // open device
    if(fd < 0)
    {
        printf("no device found\n");
        return 0;
    }

    int nc = read(fd,buffer,4,n);

    for(;;)
    {
        int num;
        printf("Number (exit with 0) = ");
        while(scanf("%d", &num) != 1)
            ;
        printf("-- %d --\n", num);
        if(num <= 0) break;

        /* use ioctl to pass a value to the character device     */
        ioctl(fd, SUMMER_SET, &num);
        n++;
    }

    /* use ioctl to get a value from the character device   */
    ioctl(fd, SUMMER_GET, &sum);

    printf("#char = %d\n", nc);
    printf("Result = %d\n", sum);
    printf("Buffer: %s\n" , buffer);
    close(fd);                         // close device
    return 0;
}

对于包依赖版本:

var app = express(); 
var server = require('http').createServer(app); 
var io = require('socket.io')(server); 

io.set('heartbeat timeout', 4000); 
io.set('heartbeat interval', 2000);