HeartbeatProtocol
Stable release
用 Java 编写的心跳协议,通过 UDP 工作
心跳协议的结构基于 UDP 数据报的连续通知,延迟为 1000 毫秒。有效负载字段包含来自客户端的标识符 ID,该标识符可以由字符串(例如 ping 源当前使用的网卡的 MAC 地址)或标识符(例如用户名)组成。服务器将监听各种 ping,根据标识符对它们进行排序,并管理它们自己定义的各种状态。每当服务器注意到在 3000 毫秒的延迟后没有收到来自客户端的任何 ping 时,就会通过一个事件通知该协议的用户可能会丢失连接,建议限制或停止通过任何其他连接进行数据交换 -依赖协议,例如 TCP。如果您在 5,000 毫秒内收到此标识符,客户端将再次连接,这意味着可以恢复数据通信通道。否则客户端将被视为完全离线。如上所述,所有这些都将通过事件管理进行通知,如下所示:
@ Override
public void onHeartbeat ( HeartbeatEvent event ) {
LOGGER . log ( Level . INFO , () -> "Received heartbeat from " + event . getSource () + " in " + event . getTimeFromPreviousBeat () + " at " + event . getBeatTimeStamp ());
}
@ Override
public void onDeath ( HeartbeatEvent event ) {
LOGGER . log ( Level . INFO , () -> event . getSource () + " died after " + event . getTimeFromPreviousBeat () + " at " + event . getBeatTimeStamp ());
}
@ Override
public void onLossCommunication ( HeartbeatEvent event ) {
LOGGER . log ( Level . INFO , () -> "Communication lost of " + event . getSource () + " in " + event . getTimeFromPreviousBeat () + " at " + event . getBeatTimeStamp ());
}
@ Override
public void onReacquiredCommunication ( HeartbeatEvent event ) {
LOGGER . log ( Level . INFO , () -> "Communication reacquired of " + event . getSource () + " in " + event . getTimeFromPreviousBeat () + " at " + event . getBeatTimeStamp ());
}
@ Override
public void onAcquiredCommunication ( HeartbeatEvent event ) {
LOGGER . log ( Level . INFO , () -> event . getSource () + " connected at " + event . getBeatTimeStamp ());
}
上面的示例代码显示了对客户端状态上的各种事件的捕获,然后实现侦听器的人员的任务将是决定在捕获某种类型的事件时做什么,如下所示:
public class ServerStart implements HeartbeatListener {
private static final ServerStart thisInstance = new ServerStart ();
public static void main ( String [] args ) {
HeartbeatProtocolManager heartbeatProtocolManager = new HeartbeatProtocolManager ( thisInstance );
heartbeatProtocolManager . addHost ( "Test" , 43210 ); //identifier, port
heartbeatProtocolManager . addHost ( "Test1" , 43211 );
}
@ Override
public void onHeartbeat ( HeartbeatEvent event ) {
LOGGER . log ( Level . INFO , () -> "Received heartbeat from " + event . getSource () + " in " + event . getTimeFromPreviousBeat () + " at " + event . getBeatTimeStamp ());
}
@ Override
public void onDeath ( HeartbeatEvent event ) {
LOGGER . log ( Level . INFO , () -> event . getSource () + " died after " + event . getTimeFromPreviousBeat () + " at " + event . getBeatTimeStamp ());
}
@ Override
public void onLossCommunication ( HeartbeatEvent event ) {
LOGGER . log ( Level . INFO , () -> "Communication lost of " + event . getSource () + " in " + event . getTimeFromPreviousBeat () + " at " + event . getBeatTimeStamp ());
}
@ Override
public void onReacquiredCommunication ( HeartbeatEvent event ) {
LOGGER . log ( Level . INFO , () -> "Communication reacquired of " + event . getSource () + " in " + event . getTimeFromPreviousBeat () + " at " + event . getBeatTimeStamp ());
}
@ Override
public void onAcquiredCommunication ( HeartbeatEvent event ) {
LOGGER . log ( Level . INFO , () -> event . getSource () + " connected at " + event . getBeatTimeStamp ());
}
}
生成的事件除了包含有效负载之外,还将包含相对于前一个事件传递的延迟,以及其自己的有效负载标识符和当前接收 ping 的时间戳。
使用此协议将允许更好地管理客户端和服务器之间的连接状态,并更好地控制可能的错误。