Socket.io 自动超时和传输错误

GRI*_*NHA 5 javascript node.js socket.io

我正在使用 node-Js(0.10.33)、socket.io 服务器和客户端(均为 1.2.1)和 express(3.18.4)构建聊天应用程序。我正在使用反向代理来隐藏 url(example.com:8083)。我一直面临自动超时的问题,分别导致传输错误和传输关闭。以下是我正在使用的文件: server.js

var app = require('express')();
var http = require('http').createServer(app);
var io = chat = require('socket.io')(http, {
  'polling duration' : 10,
  'heartbeat interval': 25,
  'heartbeat timeout': 99999,
  'close timeout': 86400,
  'transports' : ["polling", "websocket"]
});

var configurations = {};

var configurations = {
  config: fs.readFileSync("./config.json").toString()
};
var configData = JSON.parse(configurations.config);
// Configure Application IP and PORTS.
app.set('port', configData.PORT || 8080);
app.set('hostName', configData.HOST || "127.0.0.1");

// Connect to the server and listen.
http.listen(app.get('port'), app.get('hostName'), function(){
  console.log('Express server listening on  Host: ' + app.get('hostName') + ' and port ' + app.get('port'));
});

// Server connection to chat system.
chat.on('connection', function (socket) {
  // New user Join to chat.
  socket.on('new user', function (userData) {
    //New user joins
  });

  // User Left the chat.
  socket.on('disconnect', function (data) {
    // Page refresh
  });

  // User removed from the private chat list by the initiator.
  socket.on('remove user', function (userData) {
    // User is removed forcefully
  });

  // User is typing a message. Send this information to client.
  socket.on("typing", function(data) {
    // User is typing a message.
  });

  // On new message receive.
  socket.on('new message', function (message, isInstructor) {
    // When the user posts a new message.
  });

  // On message deletion.
  socket.on('delete message', function (msgBlockId) {
    // Upon deleting a message
  });

  // Debug statements in time of reconnection.
  socket.on('connect_error', function (data) {
    console.log('connect_error');
    console.log(data);
  });
  socket.on('connect_timeout', function (data) {
    console.log('connect_timeout');
    console.log(data);
  });
  socket.on('reconnect_error', function (data) {
    console.log('reconnect_error');
    console.log(data);
  });

});
Run Code Online (Sandbox Code Playgroud)

客户端.js

var timeout = undefined;
var $timeOutVal = undefined;
var hostName = Drupal.settings.config;
var max_socket_reconnects = '';

// Socket configurations;
var socket = io.connect('http://' + hostName, {
  'path': '/chat-connector',
  'forceNew': true,
  'reconnection': true,
  'reconnectionDelay': 1000,
  'reconnectionDelayMax' : 5000,
  'reconnectionAttempts': 5
});
//tell socket.io to never give up :)
socket.on('error', function(exception){
  console.log("Error occ");
  console.log(exception);
  socket.socket.connect();
});

(function ($) {
  // Calling events.
}(jQuery));


/**
 * Functions to be implemented upon socket connection.
 */
socket.on('connect', function (data) {

  socket.on('usernames', function(data) {
    // Updating user list upon addition of user
  });

  socket.on('broadcast message', function (data) {
    // Posting messages.
  });

  socket.on('updated usernames', function (data) {
    // Updating user list upon deletion of user
  });

  socket.on('notify', function (data) {
    // Posting notifiaction messages
  });

  socket.on('updated messages', function (data) {
    // Updating message board
  });

  socket.on('post remove user', function (data) {
    // Addressing an event
  });

  socket.on("reconnecting", function(delay, attempt) {
    if (attempt === max_socket_reconnects) {
      setTimeout(function(){ socket.socket.reconnect(); }, 5000);
      return console.log("Failed to reconnect. Lets try that again in 5 seconds.");
    }
  });

});


/**
 * Function to set a timeout for chat typing message display toggle.
 */
function timeoutFunction() {
  typing = false;
  socket.emit("typing", false);
}
Run Code Online (Sandbox Code Playgroud)

我收到不需要的 ping 超时,并且在某些 ping 超时后发生传输错误,这导致客户端从聊天室中掉线。还通过控制台检查,我收到以下错误:

WebSocket 连接到“ws://example.com/chat-c​​onnector/?EIO=3&transport=websocket”失败:WebSocket 握手期间出错:意外响应代码:400

但是这个错误是间歇性的。请提出建议,以便我可以解决此问题。

yun*_*nas 2

  1. 验证客户端和服务器是否具有相同的socket.io版本
  2. 验证目标端口未被阻止