Skip to content

Instantly share code, notes, and snippets.

@wnds
Created April 20, 2017 17:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wnds/4d5388a91f82bbf9f39ea300494bdb6d to your computer and use it in GitHub Desktop.
Save wnds/4d5388a91f82bbf9f39ea300494bdb6d to your computer and use it in GitHub Desktop.
package org.vivek.j2ee.websockets.client;
import javax.websocket.*;
import java.io.IOException;
import java.nio.ByteBuffer;
public class ClientWebSocketEndpoint extends Endpoint {
@Override public void onOpen(final Session session, EndpointConfig config) {
try {
session.getBasicRemote().sendText("Joining");
} catch (IOException e) {
e.printStackTrace();
}
session.addMessageHandler(new MessageHandler.Whole < String > () {
@Override public void onMessage(String message) {
System.out.println(message);
}
public void onMessage(PongMessage pongMessage) {
System.out.println(pongMessage);
}
});
new Thread(new Runnable() {
@Override public void run() {
ByteBuffer buffer = ByteBuffer.allocate(1);
buffer.put((byte) 0xFF);
try {
session.getBasicRemote().sendPing(buffer);
Thread.sleep(2000);
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment