我遇到了奇怪的WebSocket問題:在node.js的的WebSocket無法發送/接收的字符串長度超過65536個字符
服務器:
var websocket = require('websocket');
var http = require('http');
var transportServer = http.createServer(function(request, response) {});
var wsServer = new websocket.server({ httpServer: transportServer });
wsServer.on('request',function(request) {
var connection = request.accept(null,request.origin);
console.log('connected');
connection.on('message',function(message) {
console.log('message received');
});
connection.on('close',function(){
console.error('connection closed');
process.exit(0);
});
});
transportServer.listen(5000);
客戶端的瀏覽器:
var ws = new ReconnectingWebSocket 'ws://localhost:5000'
ws.onopen = function(){
var buf = '';
for(var i = 1; i <= 65536; ++i) buf = buf + 'a';
ws.send(buf);
}
例上述作品,但如果我在for循環改變65536
到65537
失敗 - 服務器不打印「消息接收」並打印「連接關閉」,而不是和沒有錯誤報告在服務器或客戶端上。 WebSocket中是否有最大消息長度?
4MiB爲32位? 4GiB你的意思是? –