Example usage for org.springframework.web.socket.handler TextWebSocketHandler TextWebSocketHandler

List of usage examples for org.springframework.web.socket.handler TextWebSocketHandler TextWebSocketHandler

Introduction

In this page you can find the example usage for org.springframework.web.socket.handler TextWebSocketHandler TextWebSocketHandler.

Prototype

TextWebSocketHandler

Source Link

Usage

From source file:ch.rasc.wampspring.demo.client.Publisher.java

public static void main(String[] args) throws InterruptedException {

    WebSocketClient webSocketClient = new StandardWebSocketClient();
    final JsonFactory jsonFactory = new MappingJsonFactory(new ObjectMapper());

    final CountDownLatch welcomeLatch = new CountDownLatch(1);
    final CountDownLatch latch = new CountDownLatch(1_000_000);
    TextWebSocketHandler handler = new TextWebSocketHandler() {

        @Override//from  w  w  w.jav  a2  s  .  c om
        protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
            WampMessage wampMessage = WampMessage.fromJson(jsonFactory, message.getPayload());

            if (wampMessage instanceof WelcomeMessage) {
                latch.countDown();
            }

        }

    };

    Long[] start = new Long[1];
    ListenableFuture<WebSocketSession> future = webSocketClient.doHandshake(handler,
            "ws://localhost:8080/wamp");
    future.addCallback(wss -> {

        // Waiting for WELCOME message
        try {
            welcomeLatch.await(5, TimeUnit.SECONDS);

            start[0] = System.currentTimeMillis();
            for (int i = 0; i < 1_000_000; i++) {
                PublishMessage publishMessage = new PublishMessage("/test/myqueue", i);
                try {
                    wss.sendMessage(new TextMessage(publishMessage.toJson(jsonFactory)));
                } catch (Exception e) {
                    System.out.println("ERROR SENDING PUBLISH_MESSAGE" + e);
                }
                latch.countDown();
            }

        } catch (Exception e1) {
            System.out.println("SENDING PUBLISH MESSAGES: " + e1);
        }

    }, t -> {
        System.out.println("DO HANDSHAKE ERROR: " + t);
        System.exit(1);
    });

    if (!latch.await(3, TimeUnit.MINUTES)) {
        System.out.println("SOMETHING WENT WRONG");
    }
    System.out.println((System.currentTimeMillis() - start[0]) / 1000 + " seconds");
}

From source file:ymanv.forex.SockJsClientTest.java

private static WebSocketHandler createWebSocketHandler() {
    return new TextWebSocketHandler() {
        @Override/* w w w .j a  v a 2 s . c  om*/
        protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
            LOG.info(message.getPayload());
        }
    };
}

From source file:com.devicehive.websockets.WebSocketApiInfoHandlerTest.java

@Test
public void shouldReturnApiInfo() throws Exception {
    final String requestId = "62345vxgsa5";

    CompletableFuture<TextMessage> future = new CompletableFuture<>();
    new StandardWebSocketClient().doHandshake(new TextWebSocketHandler() {
        @Override/*from  w w  w  .  j  a va  2 s  .  c om*/
        protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
            future.complete(message);
        }
    }, wsBaseUri() + "/websocket/client").addCallback(session -> {
        JsonObject apiInfoRequest = JsonFixture.createWsCommand("server/info", requestId);
        try {
            session.sendMessage(new TextMessage(gson.toJson(apiInfoRequest)));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }, future::completeExceptionally);

    future.thenAccept(response -> {
        JsonObject jsonResp = gson.fromJson(response.getPayload(), JsonObject.class);

        assertThat(jsonResp.get("action").getAsString(), is("server/info"));
        assertThat(jsonResp.get("requestId").getAsString(), is(requestId));
        assertThat(jsonResp.get("status").getAsString(), is("success"));
    }).exceptionally(e -> {
        fail(e.getMessage());
        return null;
    }).get(5, TimeUnit.SECONDS);
}

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/* w w w .  j a va 2  s.c o m*/
            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();
        }
    }
}