我正在使用node-Js(0.10.33),socket.io服务器和客户端(均为1.2.1)和express(3.18.4)构建聊天应用程序。我正在使用反向代理来隐藏网址(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超时之后发生了传输错误,导致客户端从聊天室中删除。 还通过控制台检查,我收到以下错误:
WebSocket连接到'ws://example.com/chat-connector/?EIO = 3& transport = websocket'失败:WebSocket握手期间出错:意外响应代码:400
但这个错误是间歇性的。请建议以便我可以解决此问题。
答案 0 :(得分:1)