NodeJS with Socket.IO delay emitting data

前端 未结 4 1994
春和景丽
春和景丽 2020-12-17 04:08

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

4条回答
  •  感情败类
    2020-12-17 04:34

    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.

    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.

提交回复
热议问题