Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package parent;
import java.io.IOException;
import java.net.URI;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import javax.websocket.ClientEndpointConfig;
import javax.websocket.Endpoint;
import javax.websocket.EndpointConfig;
import javax.websocket.MessageHandler;
import javax.websocket.Session;
import org.glassfish.tyrus.client.ClientManager;
import org.glassfish.tyrus.container.jdk.client.JdkClientContainer;
public class Main {
private static CountDownLatch messageLatch;
public static void main(String[] args) throws IOException {
try {
messageLatch = new CountDownLatch(1);
ClientManager client = ClientManager.createClient(JdkClientContainer.class.getName());
client.connectToServer(new Endpoint() {
@Override
public void onOpen(Session session, EndpointConfig config) {
try {
session.addMessageHandler(new MessageHandler.Whole<String>() {
@Override
public void onMessage(String message) {
System.out.println("Received message: "+message);
messageLatch.countDown();
}
});
session.getBasicRemote().sendText("155-questions-active");
} catch (IOException e) {
e.printStackTrace();
}
}
}, ClientEndpointConfig.Builder.create().build(), new URI("wss://qa.sockets.stackexchange.com/"));
messageLatch.await(100, TimeUnit.SECONDS);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment