如何在带有套接字(socket.io)的NodeJS中找到客户端的响应时间(延迟)? [英] How can I find the response time (latency) of a client in NodeJS with sockets (socket.io)?

查看:334
本文介绍了如何在带有套接字(socket.io)的NodeJS中找到客户端的响应时间(延迟)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用NodeJS创建多人游戏,并且希望在客户端之间同步操作.

I'm trying to create a multiplayer game with NodeJS and I want to synchronize the action between clients.

找到客户端和服务器之间的延迟(请求返回客户端的时间)的最佳方法是什么?

What would be the best way to find the latency (the time that a request take to come back to the client) between the client and the server?

我的第一个想法是客户端#1可以发送带时间戳的is请求,因此当客户端#2将收到客户端#1的操作时,他将调整操作速度以消除请求的延迟. 但是问题在于,两个客户端的系统日期时间可能不同,因此不可能两个知道客户端#1的请求的卷纸延迟.

My first idea was that the client #1 could send a timestamp with is request, so when client #2 will receive the action of the client #1 he will adjust is action speed to remove the delay of the request. But the problem is that maybe the system date time of the two clients are not identical so it is not possible two know the reel delay on the request of client #1.

另一种解决方案是使用服务器的时间戳,但是现在我如何知道客户端的延迟?

The other solution was to use the timestamp of the server, but now how can I know the latency of a client?

推荐答案

我将假设您正在使用WebSockets或 Socket.IO ,因为您正在实现延迟很重要的游戏(并为此进行了标记).

I'm going to assume you are using WebSockets or Socket.IO since you are implementing a game where latency matters (and you tagged it as such).

我认为服务器可能应该为每个客户端进行测量并保持跟踪.

I would think the server should probably measure and keep track of this for each client.

您可能想要实施服务器可以请求客户端执行的某种ping操作.客户端一接收到请求,便将响应发送回服务器.然后,服务器将除以2,并更新该客户端的等待时间.您可能希望服务器定期对每个客户端执行此操作,并可能平均最后几个客户端,以免突然而短暂的峰值导致奇怪的行为.

You probably want to implement some sort of ping action that the server can request of the client. As soon as the client receives the request, it sends back a response to the server. The server then divides by 2 and updates the latency for that client. You probably want the server to do this periodically with each client and probably average the last several so that you don't get strange behavior from sudden but temporary spikes.

然后,当需要将一个客户端的消息发送(或广播)到另一个客户端时,服务器可以将客户端1的延迟添加到客户端2的延迟中,并将其作为延迟偏移量传达给客户端2作为消息的一部分.然后client2将知道client1上的事件发生在几毫秒前.

Then, when there is a message from one client that needs to be sent (or broadcast) to another client, the server can add client1's latency to client2's latency and communicate this as the latency offset to client2 as part of the message. client2 will then know that the event on client1 happened that many milliseconds ago.

在服务器上执行此操作的另一个原因是某些浏览器Javascript时间戳不正确: http://ejohn.org/blog/accuracy-of-javascript-time/.我怀疑node.js时间戳与V8(这是为数不多的准确时间戳之一)一样准确(或更准确).

An additional reason to do this on the server is that some browser Javascript timestamps are inaccurate: http://ejohn.org/blog/accuracy-of-javascript-time/. I suspect node.js timestamps are just as accurate (or more so) than V8 (which is one of the few accurate ones).

这篇关于如何在带有套接字(socket.io)的NodeJS中找到客户端的响应时间(延迟)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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