socket.io seems to be delayed on my Android phone

2019-04-12 08:08发布

问题:

EDIT 2:

I observed the same behavior using server-sent events (SSE). See example here. (Of course, it is one way). However, it seems more robust on my Nexus 5 and it doesn't lag frequently. Also it recovers/reconnects fast.


EDIT 1:

See also Websocket interval... on SO.


I'm using a Nexus 5 with Chrome 45 to communicate to my laptop via wifi through the router.

The server on my desktop is coded with node.js version 0.12.7 and I use socket.io 1.3.6. Here is the code for the server:

'use strict'
const app = require( 'express' )();

app.get( '/', function( req, res ){
    res.render( 'test_socketio_client.ejs' );
});

let httpServer = app.listen( 8000 );

const io = require( 'socket.io' )( httpServer );
let i = 0;

io.on( 'connection', function( socket ){
    socket.on( 'req', function( data ){
        console.log( data );
        socket.emit( 'res', {res:'From server', i:i.toString()} );
        ++i;
    });
});

Here is the code for my client:

<html>
<head>
    <meta charset="utf-8"/>
    <title>Test Socket.io</title>
</head>
<body>
    <h1>Test Socket.io</h1>
    <hr/>
    <p>From server: <span id="test1"></span></p>

    <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
    <script src="/socket.io/socket.io.js"></script>
    <script>
        var socket = io();
        var i = 0;

        var f = function(){
                socket.emit( 'req', {req:'From client', 
                    i:i.toString()} );
            };

        socket.on( 'res', function( data ){
            $("#test1").html( data.i );
            ++i;
            setTimeout( f, 1000 );
        });

        f();

    </script>

</body>
</html>

If I use the browser on my desktop with http://localhost:8000 there is no problem but when I use my Nexus 5/Chrome 45 through my home wifi network, socket.io seems to behave by "chunks". As you see, my client/server perform a "ping pong" game, emitting a new message after 1 second. The fact is that I receive the ~10 first messages correctly (i.e. i increases from 0 to 10 in ten seconds) then it blocks for few seconds, then it resumes to transmit. Note that I keep, of course, the page on the top on my phone, it doesn't enter in pause state (?)

My question is: how to avoid that, to make sure an emit will be sent over the web socket as soon as it is called? Did I miss something with socket.io setups? Do I ignore something about web sockets and this behavior is normal?

Any idea?

Thanks!

回答1:

I will answer my own question...

I tried both websockets with even only server-to-client communication and server-sent events on my mobile phones (Nexus 5 with Lollipop 5.1 and an old LG p930 with Gingerbread 2.3.5) and none of them works fine with these technos. The communication is broken after 5 ... 10 or 60 seconds but it always breaks.

I will use AJAX with repetitive queries. With a query at each 0.5 sec, I never lost the communication. It will be enough for my current project.

But I would have appreciated to see SSE and/or websockets working on Chrome 45/Android 5.1.