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

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

Introduction

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

Prototype

public void setHeaders(HttpHeaders headers) 

Source Link

Document

Provide default headers to add to the WebSocket handshake request.

Usage

From source file:org.bitcoinrt.spring.config.ClientConfig.java

@Bean
public WebSocketConnectionManager connectionManager() {

    HttpHeaders headers = new HttpHeaders();
    headers.setOrigin("http://websocket.mtgox.com");

    WebSocketConnectionManager cm = new WebSocketConnectionManager(wsClient(), wsHandler(), WS_URL);
    cm.setHeaders(headers);
    cm.setAutoStartup(true);//from  w  w w.  j a v a 2 s  .c  o m
    return cm;
}

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//ww  w.j a  v  a2s.  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();
        }
    }
}