I was working with socket.io v1.3.6 (nodejs) and tried to emit the data from browser using the below code .
我正在使用socket.io v1.3.6(nodejs)并尝试使用以下代码从浏览器发出数据。
Client Code
var socket = io.connect('http://something.com:3300/');
function sendMessage(message) {
socket.emit('message', message);
}
Server Code
var io = require('socket.io').listen(3300);
io.sockets.on('connection', function (socket) {
messageHandler(socket);
});
function messageHandler(socket) {
socket.on('message', function (data) {
console.log('Captured a message : ' + data);
});
}
my socket server http://something.com:3300/ is down initially , and tried to call few sendMessage() - (say around 10 calls)
我的套接字服务器http://something.com:3300/最初是关闭的,并尝试调用少量sendMessage() - (比如大约10个调用)
As expected in the browser i will get the handshake error in console log.
正如预期在浏览器中我将在控制台日志中获得握手错误。
I waited for 5 mins and started the socket server.
我等了5分钟,启动了socket服务器。
But surprisingly all the messages sent during offline are captured in the server, once the handshake is established.
但令人惊讶的是,一旦握手建立,所有在离线期间发送的消息都将在服务器中捕获。
My questions : 1) Is this offline logic as part of socket.io or WebSocket specification ? 2) I searched a lot of offline mode socket.io questions, and saw some special handling recommendations to capture offline messages. But how this works without those special offline checks ?
我的问题:1)这个脱机逻辑是socket.io或WebSocket规范的一部分吗? 2)我搜索了很多离线模式socket.io问题,并看到一些特殊处理建议来捕获离线消息。但是如果没有那些特殊的离线检查,这怎么工
2 个解决方案
#1
5
Yes, there is an 'offline' buffering for packages to be emitted before the first connect, see here the implementation of socket.js yourself:
是的,在第一次连接之前有一个'离线'缓冲包,在这里看看socket.js的实现:
https://github.com/socketio/socket.io-client/blob/master/lib/socket.js
(especiall check onconnect and emitBuffered functions/attribs)
(特别检查onconnect和emitBuffered函数/属性)
#2
2
I solved this problem with this little change on reconnecting:
我通过重新连接时的这个小改动解决了这个问题:
mySocket.on('reconnect', function() {
//Avoid that buffered emits getting send again
mySocket.sendBuffer=[];
...
});
By setting the sendBuffer to an empty array you avoid sending the packages again on reconnect. Of course you should then handle the sending attempts on your own.
通过将sendBuffer设置为空数组,可以避免在重新连接时再次发送包。当然,您应该自己处理发送尝试。
#1
5
Yes, there is an 'offline' buffering for packages to be emitted before the first connect, see here the implementation of socket.js yourself:
是的,在第一次连接之前有一个'离线'缓冲包,在这里看看socket.js的实现:
https://github.com/socketio/socket.io-client/blob/master/lib/socket.js
(especiall check onconnect and emitBuffered functions/attribs)
(特别检查onconnect和emitBuffered函数/属性)
#2
2
I solved this problem with this little change on reconnecting:
我通过重新连接时的这个小改动解决了这个问题:
mySocket.on('reconnect', function() {
//Avoid that buffered emits getting send again
mySocket.sendBuffer=[];
...
});
By setting the sendBuffer to an empty array you avoid sending the packages again on reconnect. Of course you should then handle the sending attempts on your own.
通过将sendBuffer设置为空数组,可以避免在重新连接时再次发送包。当然,您应该自己处理发送尝试。