socket.io(websockets)的延迟问题 [英] latency issue with socket.io(websockets)

查看:267
本文介绍了socket.io(websockets)的延迟问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正计划使用node.js创建一个实时SPA,并且正在测试延迟,请通过以下链接进行操作:

we are planning to create a real time SPA using node.js and we are testing for the latency, please go through the following link :

http://173.200.239.98:6060/

有2个区域...当用户将鼠标悬停在右侧的文本区域上时,我们在左侧的文本区域中打印等待时间,并且等待时间以毫秒为单位.问题是等待时间从0.3秒到6秒不等..Web套接字是否正常?还是我做错了什么?

there are 2 area...when user hover the mouse over a textarea in right side, we are printing the latency in the left side textarea and the latency is in milliseconds. the ques is that latency varies from 0.3 secs to 6 secs..is it normal with web sockets? or am i doing something wrong?

注意:-服务器在美国底特律,我正在从印度钦奈访问服务器.

NOTE:- the server is in detroit,usa and i am accessing the server from india chennai.

源代码:

    <!DOCTYPE html>
     <html>
  <head>
    <meta charset='utf-8'/>
  </head>
  <body style='margin:0px' >

    <table>
    <tr>
        <th>latency in milliseconds</th>
        <th>hover to trigger message</th>
    </tr>
    <tr>
        <td><textarea id='message' name='message' rows="20" cols="20" ></textarea></td>
        <td><textarea id='hover' name='hover' rows="20" cols="20" ></textarea></td>
    </tr>
    </table>

    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script src="/socket.io/socket.io.js"></script>
    <script>
        jQuery(function($){

            var socket = io.connect();
            var messagearea=document.getElementById("message");
            var workarea=document.getElementById("hover");
            workarea.onmousemove=function(){


                var startTime = Date.now();
                socket.emit('ping',startTime);

            };

            socket.on('pong', function(startTime) {
                var latency = Date.now() - startTime;
                messagearea.innerHTML=  messagearea.innerHTML + latency + '\n';
                messagearea.scrollTop = messagearea.scrollHeight;

            });

        });
    </script>   

  </body>
</html>

推荐答案

通过ping测试和您的应用程序,我得到的结果几乎相同.由于ping测试使您的客户端/服务器脱离了等式,并使用了众所周知的联网工具-如果您在应用程序中获得的结果与ping测试相似,则您的客户端/服务器运行良好,而不是源于延迟.

I get nearly identical results with both a ping test and your app. Since the ping test takes your client/server out of the equation and uses a well known networking tool - if you get similar results in your app as with a ping test, then your client/server are doing pretty well and not a source of the delay.

现在,当您从印度运行到底特律的测试时,您正在遍及全球的至少一半主要海洋进行联网.如果存在一些延迟,也就不足为奇了;如果延迟有所变化,也就不足为奇了.只是为了让您了解情况,您需要将一些TCP数据包从印度发送到底特律.它们通过许多不同的路由器和服务提供商以及电缆/光纤传输,最终到达底特律的服务器.因为它是TCP,所以必须沿着相反的路线发送回送确认.然后,您的服务器获取数据包,然后以另一种方式将TCP数据包发送回去,这又需要通过返回数据包进行确认.

Now when you run the test from India to Detroit, you're doing networking half way around the world and across at least one major ocean. It's not surprising if there is some latency and not surprising if that latency varies. Just to give you an idea what has to happen is that you send some TCP packets from India to Detroit. They travel through many, many different routers and service providers and cables/fibers to eventually get to the server in detroit. Because it's TCP, confirmation of delivery has to be sent back along the reverse route. Then your server gets the packet and does it's thing sending TCP packets back the other way which again need to be confirmed with return packets.

我运行了您的应用并获得了以下结果:

I ran your app and got these results:

我从位于加利福尼亚州圣何塞的所在地进行了ping -n 20 173.200.239.98的ping测试,并得到了以下结果:

I ran a ping test with ping -n 20 173.200.239.98 from my location in San Jose, CA and got these results:

如您所见,Ping测试时间为73-87ms.您的应用游戏74-83ms.这些基本上是相同的.看来您的客户端和服务器运行正常.

As you can see, the ping test gave 73-87ms. Your app game 74-83ms. Those are basically identical. It seems like your client and server are doing just fine.

我建议您从自己的位置运行自己的ping测试,并查看ping测试和应用结果之间有多大的差异.

I'd suggest you run your own ping test from your location and see how much of a difference there is between your ping test and your app results.

如果您想查看数据包所走的路线以及到达每个站点的运输时间信息,可以运行以下命令:

If you want to see the route that your packets take with some info on transit time to each stop, you can run this command:

tracert 173.200.239.98

它会向您显示到达目的地的每个连续主要跳的时间(以毫秒为单位).

And it will show you the time (in ms) to each successive major hop along the way to the destination.

这篇关于socket.io(websockets)的延迟问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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