当前位置: 首页 > 知识库问答 >
问题:

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

邢晗日
2023-03-14

我正在使用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超时后,传输错误正在发生,这导致客户端从聊天室下降。同样通过控制台检查,我得到以下错误:

共有1个答案

柳仲卿
2023-03-14
  1. 验证客户端和服务器是否具有相同的socket.io版本
  2. 验证目的地端口没有被阻塞
 类似资料:
  • 问题内容: 我正在使用Socket.IO(最新版本1.1.0)与Android应用(客户端)交换消息。我想设置一个超时时间(例如5秒)来检查我的客户端是否仍处于连接状态(我想处理Android应用崩溃时的情况)。此外,当此超时发生时,我想生成一个事件。我想做的事情看起来像这样: 1 /设置超时 2 /处理超时事件: 但是我找不到任何实现超时的实现。 谢谢你的帮助 ! 问题答案: 这不是超时设置所处

  • 我正在使用GWT和Spring controller来管理http流量。有些请求可能需要很长时间,但我希望在超过给定时间时终止请求。 我如何配置超时Spring。我也使用Apache Tomcat 7.0。我试图在tomcat上inrease最大线程,但有一段时间tomcat工作缓慢,因为请求线程不会死。

  • 我的LogCat: 签名密钥(sw)为https://api.dropbox.com/1/shares/dropbox/a.jpg?oauth_consumer_key=2f2y1dyuqhp58ek 我对http没有太多经验。。 因为httpPost=新的httpPost(sw);工作正常,这是否意味着基本字符串签名正确? 还是我错过了什么?

  • 这个问题与套接字有关。io版本 我在两周内测试节点js并 socket.io。当我开始时,我从客户端的函数中得到了问题。我无法向服务器发送任何消息。但我仍然可以从服务器接收消息。当我发现服务器端的配置传输时,我解决了这个问题: 一切都很好。现在,我也可以向服务器发送消息。但我仍然有一个问题,为什么我必须配置传输。默认 socket.io 使用 websocket 传输设置,如下所示: 所以它首先使

  • 尝试从Angular 8应用程序调用服务时出错。服务内容如下: 下面是Java rest api(从浏览器调用时工作正常): 我在chrome控制台中发现错误: 错误:{error:SyntaxError:JSON中位于XMLHtt处JSON.parse()位置0处的意外标记A…,文本:“AAA BBB CCC”}头:HttpHeaders{normalizedNames:Map(0),lazyU

  • 我正在从我的角UI调用Spring引导REST服务。只要Spring Boot Rest服务作为Spring Boot应用程序执行,它就运行良好。但是一旦我将其转换为WAR文件并部署在Jboss 6.2.4服务器上,我就会得到404。我看到来自UI的REST服务调用成功,但请求JSON没有通过。在请求JSON上,我正在传递2个字符串和一个上传的excel文件。 这是我的angular UI htt