Created
January 18, 2016 20:09
-
-
Save Tunaki/620cda297b92082664cc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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