Socket.io断开连接

时间:2016-05-03 16:10:08

标签: angularjs node.js apache express

我在nodejs,Express,socket.io和angular中进行了聊天。它运行良好,但有时和随机时间断开连接。通常连接持续时间不超过2分钟。我在控制台上得到了几个net :: ERR_CONNECTION_TIMED_OUT。

PS:我在CentOS上使用带有认证ssl的apache 2.2。 有小费吗?

我的server.js标题位于

之下

#!/bin/env node

var express = require('express'),
path = require('path'),
app = express(),
logger = require('morgan'),
_m = require("./models/Message"),
Message = _m.m,
NewMessage = _m.n,
Group = _m.g,
Online = _m.o,
DeletedMessage = _m.d,
LastMessage = _m.l,
_mTASK = require("./models/Task"),
_Task = _mTASK.t,
TaskComment = _mTASK.c,
TaskLog = _mTASK.l,
TaskModel = _mTASK.m,
TaskNotification = _mTASK.n,
_d = require("./lib/Connection");

app.use(logger('dev'));
app.set('port', 3000);
app.set('ipaddr', "127.0.0.1");

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

// catch 404 and forward to error handler
app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

if (app.get('env') === 'development') {
    app.use(function(err, req, res, next) {
        res.status(err.status || 500);
        res.render('error', {
            message: err.message,
            error: err
        });
    });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
        message: err.message,
        error: {}
    });
});

server.listen(app.get('port'), app.get('ipaddr'), function() {
    console.log('Express server listening on  IP: ' + app.get('ipaddr') + ' and port ' + app.get('port'));
});


var io = require("socket.io")(server);

io.set("origins", 'https://meusite.me:*');
io.set('transports', ['polling', 'websocket']);

1 个答案:

答案 0 :(得分:0)

一点socket.io背景

服务器每X秒向客户端发送一次心跳,其中X == heartbeat interval配置值。

如果客户端无法响应,socket会认为连接已死

客户端每隔N秒等待来自服务器的心跳,其中N == heartbeat timeout配置值。

在打开单个连接时,将heartbeat timeout发送到客户端的服务器上设置这两个值。

鉴于上述文件,您可以将heartbeat timeout设置为......

//WARNING io.set() has been depricated

var io = require("socket.io")(server);

io.set('heartbeat interval', '30000');
io.set('heartbeat timeout', '45000');
io.set("origins", 'https://meusite.me:*');
io.set('transports', ['polling', 'websocket']);

//Setting your server configuration is now done via ..

var socket = require('socket.io')({
  // options go here
  'configOption': 'configValue';
});