private PingManager(final Connection connection) { this.connection = connection; instances.put(connection, this); ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection); sdm.addFeature(NAMESPACE); PacketFilter pingPacketFilter = new PacketTypeFilter(Ping.class); connection.addPacketListener(new PacketListener() { /** * Sends a Pong for every Ping */ public void processPacket(Packet packet) { if (pingMinDelta > 0) { // Ping flood protection enabled long currentMillies = System.currentTimeMillis(); long delta = currentMillies - lastPingStamp; lastPingStamp = currentMillies; if (delta < pingMinDelta) { return; } } Pong pong = new Pong((Ping)packet); connection.sendPacket(pong); } } , pingPacketFilter); connection.addConnectionListener(new ConnectionListener() { @Override public void connectionClosed() { maybeStopPingServerTask(); } @Override public void connectionClosedOnError(Exception arg0) { maybeStopPingServerTask(); } @Override public void reconnectionSuccessful() { maybeSchedulePingServerTask(); } @Override public void reconnectingIn(int seconds) { } @Override public void reconnectionFailed(Exception e) { } }); maybeSchedulePingServerTask(); }
private void init() { periodicPingExecutorService = new ScheduledThreadPoolExecutor(1); PacketFilter pingPacketFilter = new PacketTypeFilter(Ping.class); connection.addPacketListener(new PacketListener() { /** * Sends a Pong for every Ping */ public void processPacket(Packet packet) { if (pingMinDelta > 0) { // Ping flood protection enabled long currentMillies = System.currentTimeMillis(); long delta = currentMillies - lastPingStamp; lastPingStamp = currentMillies; if (delta < pingMinDelta) { return; } } Pong pong = new Pong((Ping)packet); connection.sendPacket(pong); } } , pingPacketFilter); connection.addConnectionListener(new ConnectionListener() { @Override public void connectionClosed() { maybeStopPingServerTask(); } @Override public void connectionClosedOnError(Exception arg0) { maybeStopPingServerTask(); } @Override public void reconnectionSuccessful() { maybeSchedulePingServerTask(); } @Override public void reconnectingIn(int seconds) { } @Override public void reconnectionFailed(Exception e) { } }); instances.put(connection, this); maybeSchedulePingServerTask(); }