Skip to content
This repository was archived by the owner on Jul 21, 2020. It is now read-only.
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
Expand All @@ -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.
Expand All @@ -73,6 +112,9 @@ public void disconnect(int code, String msg) {
}

this.connection.closeConnection(code, msg);
if (pingTimer != null) {
pingTimer.cancel();
}
}
}

Expand Down Expand Up @@ -133,4 +175,4 @@ public void delete(IncomingMessageData message) {
this.connection.delete(message);
}
}
}
}