简体   繁体   English

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

[英]NodeJS with Socket.IO delay emitting data

I am using the example from the Socket.IO homepage (http://socket.io/). 我正在使用Socket.IO主页(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. 它可以工作,但是在发送数据和在另一端接收数据之间存在很大的延迟。

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>

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. 这会强制服务器使用WebSockets,Flash套接字或长轮询。 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. rememberTransport强制服务器和客户端忘记它最后使用的连接,并尝试连接上面的“传输”。

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. 到socket构造函数。 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. 多谢你们。

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个字节,则在刷新缓冲区之前可能会有一些等待时间,因为它没有被填充。

Using websockets improves the behavior in the sense that it disables buffering. 使用websockets改进了行为,因为它禁用了缓冲。 Websockets is implemented with setNoDelay(true) as can be seen in the websockets code so it will not buffer messages. Websockets是使用setNoDelay(true)实现的,如websockets代码中所示,因此它不会缓冲消息。

You can request websockets explicitly by placing the word websocket first inside the transports array. 您可以通过将websocket首先放在transports数组中来明确请求websockets。 On recent versions of socket.io and engine.io the correct arguments and implementation looks like this: 在最新版本的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'] });

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套接字或闪存轮询慢得多。

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

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