Websocket 间隔:移动设备上的性能问题? [英] Websocket interval: performance problems on mobile devices?

查看:29
本文介绍了Websocket 间隔:移动设备上的性能问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我当前的项目中,我发现在移动设备上使用带有 socket.io 和 node.js 的 websockets 的问题.看来移动设备在一个时间间隔内处理套接字消息有问题.

In my current project I discovered a problem using websockets with socket.io and node.js on mobile devices. It seems that there is a problem for mobile deviced handling socket messages in an interval.

我将其简化为最小场景:

I reduced it to a minimal scenario:

服务器(最小的 express.js 服务器)以特定的时间间隔向客户端发送消息:

The server (minimal express.js server) sends messages to the client in an specific interval:

setInterval(function(){
  socket.emit('interval');
}, 500);

客户端只是测量收到消息之间的时间并显示它们:

The client just messures the time between the received messages and displays them:

socket.on('interval', function (data) {
  timeElement.html(new Date() - startTime);
  startTime = new Date();
});

desktop(使用 Chrome)上,消息之间的结果时间非常稳定,为 515 ms.所以它就像一个 15ms 的延迟,但消息间隔是一致的.

On a desktop (using Chrome) the resulting time between the messages is pretty stable at 515 ms. So its like a 15ms delay but message interval is consistent.

移动设备(我使用的是带有 Chrome 的 Galaxy Nexus)上,时间在 400 到 600 毫秒之间变化,其中任何一种方式都有一些更极端的峰值.

On a mobile device (I'm using a Galaxy Nexus with Chrome) the time varies between 400 and 600 ms with some more extrem spikes in either way.

我想使用这样的时间间隔作为游戏回合指示器,这个问题会导致移动设备上的玩家移动出现很多滞后和不均匀.

I want to use such an interval as a game turn indicator and this problem results in a lot of lagging and uneven player movement on mobile devices.

推荐答案

  1. 某些代理不允许网络套接字连接,因此如果不允许,则您需要切换到 HTTP.
  2. 由于移动网络不可靠,您的网络套接字可能不可用,最好在特定时间段内检查连接是否处于活动状态.如果不是,则建立新的连接.

这篇关于Websocket 间隔:移动设备上的性能问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆