使用socketio心跳的优点/缺点 [英] Advantage/disadvantage of using socketio heartbeats

查看:1320
本文介绍了使用socketio心跳的优点/缺点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Socket.io允许您使用脉动信号检查Socket.IO连接的运行状况".心跳到底是什么?为什么我不应该使用它们?

Socket.io allows you to use heartbeats to "check the health of Socket.IO connections." What exactly are heartbeats and why should or shouldn't I use them?

推荐答案

心跳是定期从客户端发送到服务器(或从服务器发送到客户端再返回到服务器)的小消息,以确认客户仍然在场并且活跃.

A heartbeat is a small message sent from a client to a server (or from a server to a client and back to the server) at periodic intervals to confirm that the client is still around and active.

例如,如果您有一个为聊天室提供服务的Node.js应用程序,并且用户长时间没有说话,则无法判断他们是否仍然保持联系.通过以预定的时间间隔(例如,每15秒)发送一次声音,客户端将通知服务器它仍然存在.如果已经服务器从客户端发出心跳信号后20秒钟,很可能已断开连接.

For example, if you have a Node.js app serving a chat room, and a user doesn't say anything for many minutes, there's no way to tell if they're really still connected. By sending a hearbeat at a predetermined interval (say, every 15 seconds), the client informs the server that it's still there. If it's been e.g. 20 seconds since the server's gotten a heartbeat from a client, it's likely been disconnected.

这是必需的,因为您不能无法保证通过TCP进行干净的连接终止-如果客户端崩溃或发生其他情况,您将不会从客户端收到终止数据包,并且服务器也不会知道客户端已断开连接.此外,Socket.IO支持其他各种机制(TCP套接字除外)来传输数据,在这种情况下,客户端不会(或无法)向服务器发送终止消息.

This is necessary because you cannot be guaranteed a clean connection termination over TCP--if a client crashes, or something else happens, you won't receive the termination packets from the client, and the server won't know that the client has disconnected. Furthermore, Socket.IO supports various other mechanisms (other than TCP sockets) to transfer data, and in these cases the client won't (or can't) send a termination message to the server.

默认情况下,Socket.IO客户端每15秒(心跳间隔)就会向服务器发送心跳,如果20秒钟后仍未从客户端收到服务器的消息,则 >心跳超时),它将认为客户端已断开连接.

By default, a Socket.IO client will send a heartbeat to the server every 15 seconds (heartbeat interval), and if the server hasn't heard from the client in 20 seconds (heartbeat timeout) it will consider the client disconnected.

我想不出很多普通的使用案例,在这些案例中,您可能不想使用心跳信号.

I can't think of many average use cases where you probably wouldn't want to use heartbeats.

这篇关于使用socketio心跳的优点/缺点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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