diff --git a/src/main/java/com/mixer/api/resource/chat/ws/MixerChatConnectable.java b/src/main/java/com/mixer/api/resource/chat/ws/MixerChatConnectable.java index 95c7de4..786ec77 100644 --- a/src/main/java/com/mixer/api/resource/chat/ws/MixerChatConnectable.java +++ b/src/main/java/com/mixer/api/resource/chat/ws/MixerChatConnectable.java @@ -11,7 +11,13 @@ import com.mixer.api.resource.chat.replies.ReplyHandler; import javax.net.ssl.SSLSocketFactory; + +import org.java_websocket.framing.Framedata; +import org.java_websocket.framing.FramedataImpl1; + import java.io.IOException; +import java.util.Timer; +import java.util.TimerTask; public class MixerChatConnectable { private static final SSLSocketFactory SSL_SOCKET_FACTORY = (SSLSocketFactory) SSLSocketFactory.getDefault(); @@ -23,7 +29,9 @@ public class MixerChatConnectable { private final Object connectionLock = false; private MixerChatConnection connection; private AuthenticateMessage auth; - + + private Timer pingTimer; + public MixerChatConnectable(MixerAPI mixer, MixerChat chat) { this.mixer = mixer; this.chat = chat; @@ -49,9 +57,40 @@ public boolean connect() { } this.connection = newConnection; + handlePing(); + return true; } } + + /** + * Handles the automatic ping to the socket. + */ + private void handlePing() { + if (pingTimer != null) { + pingTimer.cancel(); + } + + pingTimer = (new Timer()); + pingTimer.schedule(new TimerTask() { + @Override + public void run() { + if (connection.isOpen()) { + ping(); + } + } + }, 30 * 1000); + } + + /** + * Sends a ping to the websocket server. + */ + private void ping() + { + FramedataImpl1 frame = FramedataImpl1.get(Opcode.PING); + frame.setFin(true); + connection.sendFrame(frame); + } /** * Causes the websocket to be closed and not reconnected. @@ -73,6 +112,9 @@ public void disconnect(int code, String msg) { } this.connection.closeConnection(code, msg); + if (pingTimer != null) { + pingTimer.cancel(); + } } } @@ -133,4 +175,4 @@ public void delete(IncomingMessageData message) { this.connection.delete(message); } } -} +} \ No newline at end of file