简体   繁体   English

Socket.io 发射()延迟/缓冲区刷新?

[英]Socket.io emit() delay / buffer flush?

I'm using socket.io to send messages to browser.我正在使用socket.io向浏览器发送消息。 On node.js side I'm doing在 node.js 方面我正在做

socket.emit('message', data);

On browser-side I'm doing在浏览器端我正在做

socket.on('message', handleData);

Now this works fine.现在这工作正常。 For testing purposes I'm manually triggering (from node-inspector console) the socket.emit() .出于测试目的,我手动触发(从节点检查器控制台) socket.emit() I'm able to do this 2-3 times after which the next message takes a long time to deliver.我可以这样做 2-3 次,之后下一条消息需要很长时间才能传递。 About 10 seconds.大约 10 秒。

My messages are rather short.我的信息比较短。 Compression is enabled and the object JSON {"could be about": "this long"} .压缩已启用,对象 JSON {"could be about": "this long"} When testing with longer strings, all messages are sent instantly.使用更长的字符串进行测试时,所有消息都会立即发送。 So this has something to do with buffering/optimization, but in our case, it's important that all messages are sent instantly.所以这与缓冲/优化有关,但在我们的例子中,立即发送所有消息很重要。

Does anyone have any insight into this delay?有没有人对这种延迟有任何见解? Thanks谢谢

I had the same problem.我有同样的问题。 Although it's been years, these tips would be useful to someone else witht eh problem.尽管已经过去了很多年,但这些技巧对没有问题的其他人很有用。

  1. How To Cancel, Timeout, or Clear Queued Messages In Socket.IO 如何取消、超时或清除 Socket.IO 中的排队消息

  2. https://github.com/feathersjs/feathers/issues/1532 https://github.com/feathersjs/feathers/issues/1532

    socket.on('connect', function() { socket.sendBuffer = []; // do stuff }); socket.on('connect', function() { socket.sendBuffer = []; // 做事 });

    socket.on('reconnect', function() { socket.sendBuffer = []; }); socket.on('reconnect', function() { socket.sendBuffer = []; });

A link to the official documentation (v4.0):官方文档(v4.0)的链接:

https://socket.io/docs/v4/client-offline-behavior/ https://socket.io/docs/v4/client-offline-behavior/

Indeed there are three ways to fight buffering on client side:实际上,有三种方法可以在客户端对抗缓冲:

  • use the connected attribute of the Socket instance使用 Socket 实例的 connected 属性
if (socket.connected) { socket.emit( /* ... */ ); } else { // ... }
  • use volatile events使用可变事件
socket.volatile.emit( /* ... */ );
  • empty the internal buffer upon reconnection重新连接时清空内部缓冲区
socket.on("connect", () => { socket.sendBuffer = []; });

Docs on volatile emits:关于 volatile 的文档发出:

https://socket.io/docs/v4/emitting-events/#Volatile-events https://socket.io/docs/v4/Emitting-events/#Volatile-events

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

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