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

标签 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);
  });

});

Client.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);
}

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

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

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

最佳答案

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

关于javascript - Socket.io自动超时和传输错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27544949/

相关文章:

javascript - JavaScript 中的本地文件支持检测 - Windows Phone 8 Bug

javascript - react VX 图表 : React does not recognize the `xScale` prop on a DOM element

javascript - TypeError : callback. apply is not a function (Node.js & Mongodb)

javascript - 从 MongoDB 在 Angular.js 中显示图像

python - Flask socket IO 从另一个模块发出

javascript - 将 socket.io-client 与 webpack 一起使用时未定义全局

javascript - 使用 react-select 通过 bootstrap 4 创建标记

javascript - 如何获取图片地址

mysql - nodejs-无法将结果返回给 Controller 函数

javascript - 如果socketIO生成的ID是自动生成的,如何在两个用户之间建立通信?