简体   繁体   English

当服务器脱机时,socket.io会发出

[英]socket.io emit when server is offline

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 ? 我的问题:1)这个脱机逻辑是socket.io或WebSocket规范的一部分吗? 2) I searched a lot of offline mode socket.io questions, and saw some special handling recommendations to capture offline messages. 2)我搜索了很多离线模式socket.io问题,并看到了一些特殊处理建议来捕获离线消息。 But how this works without those special offline checks ? 但是如果没有那些特殊的离线检查,这怎么工

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 https://github.com/socketio/socket.io-client/blob/master/lib/socket.js

(especiall check onconnect and emitBuffered functions/attribs) (特别检查onconnect和emitBuffered函数/属性)

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. 通过将sendBuffer设置为空数组,可以避免在重新连接时再次发送包。 Of course you should then handle the sending attempts on your own. 当然,您应该自己处理发送尝试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM