Socket.IO - 如何在客户端更改超时值选项? [英] Socket.IO - How to change timeout value option on client side?

查看:673
本文介绍了Socket.IO - 如何在客户端更改超时值选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Socket.IO 0.9.16建立连接:

I use Socket.IO 0.9.16 to establish a connection:

io.connect(url)

然而,当服务器死机时,检测到服务器错误需要2分钟超时。

However, when the server dies, it takes 2 minutes timeout to detect error with the server.

我已经尝试通过设置将超时减少到5秒:

I already tried to reduce timeout to 5s by setting:

io.connect(url, {'timeout': 5000, 'connect_timeout': 5000})

但没有成功......我怎么能这样做?

But no success... How can I do this?

推荐答案

这里的挑战是有很多不同的设置,彼此交互和一些重试逻辑,所有这些都使socket.io超时不是你通常所期望的。我应该补充一点,我熟悉socket.io 1.0+,而不是0.9虽然这可能适用于两者。

The challenge here is that there are a bunch of different settings that interact with each other and some retry logic that all make a socket.io timeout not what you would normally expect. I should add that I am familiar with socket.io 1.0+, not 0.9 though this probably applies to both.

让我们回顾一下socket.io连接是如何工作的。

Lets review how a socket.io connection works.


  1. 它试图初始连接。

  2. 如果成功,那么你就完成了连接。

  3. 如果连接尝试没有立即返回,那么将等待您在连接结果的初始选项中传递的 timeout 值。

  4. 如果您的服务器已关闭,则连接尝试可能会很快失败。这将导致 connect_error ,如果您在套接字上使用 socket.on('connect_error',function(){..}注册该消息。 。}); ,你会看到 connect_error 事件。

  5. 这个 connect_error 不是超时。因此,如果连接快速失败(通常在服务器刚刚关闭时失败),那么您永远不会获得常规超时和传递给 io({timeout:5000})的超时参数真的不会生效。只有当与服务器的连接花费很长时间(例如过于繁忙的服务器或接受TCP连接的服务器,但响应速度很慢)时才会生效。当服务器刚刚关闭或无法访问时,通常不会发生这种情况。

  6. 因此,在socket.io获得 connect_error 后,它会标记这个socket.io连接用于重试。

  7. 重试之前的延迟是基于一大堆事情。据推测, reconnectionDelay 选项是公式的一部分,但在查看代码时,还有一种退避算法,可以延长重试之间的时间,重试次数也会增加。因此,可以说,有一些算法在重试之前计算给定的延迟,每次重试都会有所不同。

  8. 然后,在计算出的延迟之后,它会再次尝试连接。这基本上重复了从步骤1开始的过程。

  9. 我可以告诉他,默认情况下它会永远重试。您可以通过 reconnectionAttempts 选项指定最大重新连接尝试次数。如果不传递,则默认为无穷大。但是,如果你传递 10 ,那么它将在连续10次连接失败后放弃。

  10. 如果指定 reconnectionAttempts ,然后在许多连接尝试失败之后,你将在套接字上得到一个 reconnect_failed 事件,它将放弃。

  11. 正如我所知道的那样,没有传统的超时方式,你正在寻找它将连接的地方,尝试重试,然后在x时间后放弃。 超时选项仅适用于单次重新连接尝试,而不适用于它一直尝试连接的总时间。

  1. It attempts to make the initial connection.
  2. If that succeeds, then you're done with the connection.
  3. If that connection attempt does not return immediately, it will wait the timeout value that you pass in the initial options for a connection result.
  4. If your server is down, the connection attempt will likely fail quickly. This will result in a connect_error and if you register for that message on the socket with socket.on('connect_error', function() {...});, you will see that connect_error event.
  5. This connect_error is not a timeout. So, if the connection fails quickly (which it usually does when the server is just down), then you never get the regular timeout and the timeout argument you pass to io({timeout: 5000}) really doesn't come into effect. That only comes into effect when a connection to a server is just taking a long time (like an overly busy server or a server that accepted the TCP connection, but is really slow at responding). This is not usually what happens when a server is just down or unreachable.
  6. So, after socket.io gets a connect_error it marks this socket.io connection for retry.
  7. The delay before retrying is based on a whole bunch of things. Presumably, the reconnectionDelay option is part of the formula, but in looking at the code, there is also a backoff algorithm that lengthens the time between retries the more times it has retried. So, suffice it to say, there's some algorithm that calculates a given delay before retrying that varies for each retry.
  8. Then, after that calculated delay, it tries to connect again. This essentially repeats the process starting at step 1 again.
  9. As best I can tell, by default it keeps retrying forever. There is an option you can pass reconnectionAttempts that specifies the maximum number of reconnection attempts. This default to infinity if you don't pass it. But, if you pass 10, then it will give up after 10 successive connection failures.
  10. If you specify reconnectionAttempts, then after that many unsuccessful connection attempts, you will get a reconnect_failed event on the socket and it will give up.
  11. As best I can tell, there is no traditional timeout in the way that you are looking for where it would connect, attempt some retries, then give up after x amount of time. The timeout option applies only to a single reconnect attempt and not to the total amount of time it keeps trying to connect.

在我一直在试验的示例测试页面中,我能够实现我自己的传统连接超时,如下所示:

In a sample test page I've been experimenting with, I was able to implement my own traditional connection timeout like this:

var socket = io(...);

// set connect timer to 5 seconds
socket._connectTimer = setTimeout(function() {
    socket.close();
}, 5000);

socket.on('connect', function() {
    // socket connected successfully, clear the timer
    clearTimeout(socket._connectTimer);
});

无论连接尝试需要多长时间,连接成功都会等待最多5秒钟或者在那段时间内发生许多重新连接尝试。没有成功连接5秒后,它关闭了套接字。

This will wait a maximum of 5 seconds for a successful connection, regardless of how long a connection attempt takes or many reconnect attempts occur in that span of time. After 5 seconds without a successful connection, it shuts down the socket.

在我的测试应用程序中,我可以看到socket.io一遍又一遍地重试连接,直到之后5秒钟,我的计时器触发,我收到超时的通知,我关闭了套接字,它再次停止尝试重试。

In my test app, I can see socket.io happily retrying the connection over and over again until after 5 seconds, my timer fires, I get notified of the "timeout" and I close the socket and it stops trying to retry any more.

这篇关于Socket.IO - 如何在客户端更改超时值选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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