javascript 带有 Socket.IO 延迟发射数据的 NodeJS

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8350630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 03:10:06  来源:igfitidea点击:

NodeJS with Socket.IO delay emitting data

javascriptnode.jssocket.io

提问by trentr

I am using the example from the Socket.IO homepage (http://socket.io/). It works and everything, but there is a huge delay between the time data is sent, and when that data is received on the other end.

我正在使用 Socket.IO 主页 (http://socket.io/) 中的示例。它可以正常工作,但是在发送数据和另一端接收数据之间存在巨大的延迟。

I am using XAMPP, I have socket.html in my dir, and navigate to it using "http://localhost/socket.html" in my browser, and I have the server listening on port 8080.

我正在使用 XAMPP,我的目录中有 socket.html,并在我的浏览器中使用“http://localhost/socket.html”导航到它,并且我让服务器侦听端口 8080。

Server:

服务器:

var io = require('socket.io').listen(8080);

io.sockets.on('connection', function (socket) {
 socket.emit('news', { hello: 'world' });
 socket.on('my other event', function (data) {
   console.log(data);
 });
});

HTML File:

HTML文件:

<html>
<head>
    <script src="http://localhost:8080/socket.io/socket.io.js"></script>
    <script>
      var socket = io.connect('http://localhost:8080');
      socket.on('news', function (data) {
        console.log(data);
        socket.emit('my other event', { my: 'data' });
      });
    </script>
</head>

<body>

</body>
</html>

回答by trentr

I have found the problem.

我找到了问题所在。

In the server I changed:

在服务器中我改变了:

var io = require('socket.io').listen(8080);

to

var io = require('socket.io', { rememberTransport: false, transports: ['WebSocket', 'Flash Socket', 'AJAX long-polling'] }).listen(8080);

which forces the server to use either WebSockets, Flash Sockets, or long-polling. It wil try to use those in that order. The rememberTransport forces the server and client to forget which connection it used last, and try to connect with the 'transports' above.

这会强制服务器使用 WebSockets、Flash Sockets 或长轮询。它将尝试按该顺序使用它们。记住传输强制服务器和客户端忘记它最后使用的连接,并尝试与上面的“传输”连接。

On the client side I just pretty much did the same thing. I added:

在客户端,我几乎做了同样的事情。我补充说:

{ rememberTransport: false, transports: ['WebSocket', 'Flash Socket', 'AJAX long-polling']}

to the socket constructor. So it looked like:

到套接字构造函数。所以它看起来像:

var socket = io.connect('http://localhost:843', { rememberTransport: false, transports: ['WebSocket', 'Flash Socket', 'AJAX long-polling']});

Now it seems to work perfectly.

现在它似乎完美地工作。

Thanks guys.

多谢你们。

回答by msimr-aptgeek

Have you tried with a longer message?

您是否尝试过使用更长的消息?

There is surely a buffer on the socket. If you send less than X bytes there might be some wait time before the buffer is flushed since it was not filled.

套接字上肯定有一个缓冲区。如果您发送的字节数少于 X 字节,则在缓冲区刷新之前可能需要一些等待时间,因为它没有被填满。

回答by Justin Thomas

What browser are you using? Socket.IO degrades down to polling, which would be much slower than native browser web sockets or flash polling.

你使用的是什么浏览器?Socket.IO 降级为轮询,这将比本机浏览器 Web 套接字或 Flash 轮询慢得多。

回答by Gary Weiss

Using websockets improves the behavior in the sense that it disables buffering. Websockets is implemented with setNoDelay(true)as can be seen in the websockets codeso it will not buffer messages.

使用 websockets 在禁用缓冲的意义上改进了行为。setNoDelay(true)正如在websockets 代码中看到的那样,Websockets 是用它实现的,因此它不会缓冲消息。

You can request websockets explicitly by placing the word websocketfirst inside the transportsarray. On recent versions of socket.ioand engine.iothe correct arguments and implementation looks like this:

您可以通过将单词websocketfirst 放在transports数组中来显式请求 websockets 。在最新版本中socket.ioengine.io正确的参数和实现如下所示:

import socketIO from 'socket.io';
const server = express();
const requestHandler = server.listen(PORT, () => console.log(`Listening on ${PORT}`));
const io = socketIO(requestHandler, { transports: ['websocket', 'polling'] });

And on the client side:

而在客户端:

import io from 'socket.io-client';
let socket = io(SERVER_URL, { transports: ['websocket', 'polling'] });