Example usage for org.springframework.web.socket.client WebSocketConnectionManager start

List of usage examples for org.springframework.web.socket.client WebSocketConnectionManager start

Introduction

In this page you can find the example usage for org.springframework.web.socket.client WebSocketConnectionManager start.

Prototype

@Override
public final void start() 

Source Link

Document

Start the WebSocket connection.

Usage

From source file:com.kurento.kmf.jsonrpcconnector.client.JsonRpcClientWebSocket.java

private synchronized void connectIfNecessary() throws IOException {

    if (wsSession == null) {

        final CountDownLatch latch = new CountDownLatch(1);

        TextWebSocketHandler webSocketHandler = new TextWebSocketHandler() {

            @Override//from w w  w  .j  ava  2  s .c  om
            public void afterConnectionEstablished(WebSocketSession wsSession2) throws Exception {

                wsSession = wsSession2;
                rs = new WebSocketResponseSender(wsSession);
                latch.countDown();
            }

            @Override
            public void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
                handleWebSocketTextMessage(message);
            }

            @Override
            public void afterConnectionClosed(WebSocketSession s, CloseStatus status) throws Exception {

                // TODO Call this when you can't reconnect or close is
                // issued by client.
                handlerManager.afterConnectionClosed(session, status.getReason());
                log.debug("WebSocket closed due to: {}", status);
                wsSession = null;
                // TODO Start a timer to force reconnect in x millis
                // For the moment we are going to force it sending another
                // message.
            }
        };

        WebSocketConnectionManager connectionManager = new WebSocketConnectionManager(
                new StandardWebSocketClient(), webSocketHandler, url);

        connectionManager.setHeaders(headers);
        connectionManager.start();

        try {
            latch.await();

            if (session == null) {

                session = new ClientSession(null, null, JsonRpcClientWebSocket.this);
                handlerManager.afterConnectionEstablished(session);

            } else {

                String result = rsHelper.sendRequest(JsonRpcConstants.METHOD_RECONNECT, String.class);

                log.info("Reconnection result: {}", result);

            }

        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}