2015-11-04 862 views
2

我有一個socket.io nodejs應用程序(在Windows Azure中)存在問題。它工作正常,但過了一段時間我正在那裏的服務器來回復爲錯誤:Node.js - Socket.io - 「最大併發連接數」問題

HTTP/1.1 503活躍的WebSocket請求數已達到最大併發WebSocket的請求允許

服務器設置:

var client_id = 0; 
var connectCounter = 0; 

var server = http.createServer(function(req, res) { 
    res.writeHead(200, { 'Content-Type': 'text/plain' }); 
    console.log('server init.'); 
}).listen(port); 
io = io.listen(server); 

io.sockets.on('connection', function(socket) 
{ 
    socket.client_id = client_id; 
    client_id++; 
    connectCounter++; 
    console.log('A socket with client_id ' + socket.client_id + ' connected!'); 
    console.log('Sockets counter: ' + connectCounter); 

    socket.on('disconnect', function() { 
     console.log('A socket with client_id ' + socket.client_id + ' disconnected!'); 
     connectCounter--; 
     console.log('Sockets counter: ' + connectCounter); 
    }); 
}); 

實例日誌結尾:

A socket with client_id 349 connected! 
Sockets counter: 1 
A socket with client_id 350 connected! 
Sockets counter: 2 
A socket with client_id 349 disconnected! 
Sockets counter: 1 
A socket with client_id 350 disconnected! 
Sockets counter: 0 

由於我的插座連接和斷開,不應該有350個併發連接。我做錯了什麼?

+0

看看這個,顯然調用'this.transport.close()'關閉應該在ping超時時被銷燬的TCP套接字:https://github.com/socketio/socket.io/issues/1910 –

+0

也許應該問,你使用PaaS或IaaS?即,您是否完全控制運行nodeJS的虛擬機? –

回答

2

根據我的意見,問題可能存在於engine.io模塊中的一個錯誤,該模塊是socket.io的組件。表面上,原因是engine.io在檢測到ping超時時不會關閉tcp套接字。

GitHub issue I found,他們顯然通過增加self.transport.close()調用位於路徑的文件修補的bug:

node_modules/socket.io/node_modules/engine.io/lib/socket.js 

應在某處您的節點app目錄。

我在this GitHub pull找到了這行代碼。代碼如下:

Socket.prototype.setPingTimeout = function() { 
    var self = this; 
    clearTimeout(self.pingTimeoutTimer); 
    self.pingTimeoutTimer = setTimeout(function() { 
     self.onClose('ping timeout'); 
     self.transport.close(); // This is the added line 
    }, self.server.pingInterval + self.server.pingTimeout); 
}; 

試試看,它可能會解決您的問題。

+0

我已經把它做了測試,我會讓你知道結果。感謝您的發現。 – xinaris

+0

這就是問題所在!現在它似乎在20小時後正常工作。 – xinaris

+0

@xinaris難道你不喜歡互聯網嗎? –