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 的時間戳記。
使用此協定將允許更好地管理客戶端和伺服器之間的連接狀態,並更好地控制可能的錯誤。