socket.io 在我的 Android 手机上似乎有延迟

编程入门 行业动态 更新时间:2024-10-28 14:32:39
本文介绍了socket.io 在我的 Android 手机上似乎有延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

编辑 2:

我使用服务器发送的事件 (SSE) 观察到了相同的行为.请参阅此处的示例.(当然,这是一种方式).但是,它在我的 Nexus 5 上似乎更强大,并且不会经常滞后.它也可以快速恢复/重新连接.

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.

编辑 1:

另见 Websocket 间隔...关于 SO.

我使用带有 Chrome 45 的 Nexus 5 通过路由器通过 wifi 与我的笔记本电脑通信.

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

我桌面上的服务器使用 node.js 版本 0.12.7 编码,我使用 socket.io 1.3.6.这是服务器的代码:

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;
    });
});

这是我的客户的代码:

<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/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>

如果我在桌面上使用带有 http://localhost:8000 的浏览器,则没有问题,但是当我通过家庭 wifi 网络使用 Nexus 5/Chrome 45 时,socket.io 似乎按块"表现.如您所见,我的客户端/服务器执行乒乓"游戏,1 秒后发出一条新消息.事实是,我正确接收了 ~10 条第一条消息(即我在 10 秒内从 0 增加到 10),然后它阻塞了几秒钟,然后它继续传输.请注意,当然,我保留了手机顶部的页面,它不会进入暂停状态 (?)

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 (?)

我的问题是:如何避免这种情况,以确保 emit 将在调用后立即通过 Web 套接字发送?我错过了 socket.io 设置吗?我是否忽略了有关 Web 套接字的某些内容并且这种行为是正常的?

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?

有什么想法吗?

谢谢!

推荐答案

我会回答我自己的问题...

I will answer my own question...

我在我的手机(Nexus 5 with Lollipop 5.1 和一个旧的 LG p930)上尝试了 websockets,甚至只有服务器到客户端的通信和服务器发送的事件使用 Gingerbread 2.3.5)并且它们都不适用于这些技术.通信在 5 ... 10 或 60 秒后中断,但总是中断.

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.

我将使用 AJAX 进行重复查询.每 0.5 秒进行一次查询,我从未丢失过通信.对于我目前的项目来说已经足够了.

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.

但我很高兴看到 SSE 和/或 websockets 在 Chrome 45/Android 5.1 上运行.

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

这篇关于socket.io 在我的 Android 手机上似乎有延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-26 05:33:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1130443.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:机上   socket   io   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!