Example usage for javax.websocket WebSocketContainer connectToServer

List of usage examples for javax.websocket WebSocketContainer connectToServer

Introduction

In this page you can find the example usage for javax.websocket WebSocketContainer connectToServer.

Prototype

Session connectToServer(Class<?> annotatedEndpointClass, URI path) throws DeploymentException, IOException;

Source Link

Usage

From source file:org.wso2.carbon.integration.test.client.WebSocketClient.java

public void receive(String url, int retryCount) {

    receivedMessage = null;//  www .  ja  v  a  2 s  .  c om
    final URI uri = URI.create(url);
    final int tryCount = retryCount;

    new Thread(new Runnable() {
        @Override

        public void run() {
            try {

                WebSocketContainer container = ContainerProvider.getWebSocketContainer();
                try {
                    // Attempt Connect
                    Session session = container.connectToServer(EventSocketClient.class, uri);

                    int count = 0;
                    while (count < tryCount && receivedMessage == null) {
                        log.info("Waiting for the sever to send message");
                        Thread.sleep(1000);
                    }

                    session.close();
                } finally {
                    // Force lifecycle stop when done with container.
                    // This is to free up threads and resources that the
                    // JSR-356 container allocates. But unfortunately
                    // the JSR-356 spec does not handle lifecycles (yet)
                    if (container instanceof LifeCycle) {
                        ((LifeCycle) container).stop();
                    }
                }
            } catch (Throwable t) {
                log.error(t);
            }
        }
    }).start();

}

From source file:wsclient.WebsocketClientEndpoint.java

public void go() {

    try {/*from  w ww.j a v  a 2  s  . c  o  m*/
        WebSocketContainer container = ContainerProvider.getWebSocketContainer();
        container.connectToServer(this, new URI("ws://46.254.19.232:8080"));
    } catch (Exception e) {
        System.out.println("OPA");
        throw new RuntimeException(e);
    }

}