Example usage for org.springframework.web.socket TextMessage toString

List of usage examples for org.springframework.web.socket TextMessage toString

Introduction

In this page you can find the example usage for org.springframework.web.socket TextMessage toString.

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

From source file:org.kurento.jsonrpc.internal.ws.JsonRpcWebSocketHandler.java

@Override
public void handleTextMessage(final WebSocketSession wsSession, TextMessage message) throws Exception {

    try {/*from w  ww  .  j  a v a 2s .c o  m*/

        String messageJson = message.getPayload();

        // TODO Ensure only one register message per websocket session.
        ServerSessionFactory factory = new ServerSessionFactory() {
            @Override
            public ServerSession createSession(String sessionId, Object registerInfo,
                    SessionsManager sessionsManager) {
                return new WebSocketServerSession(sessionId, registerInfo, sessionsManager, wsSession);
            }

            @Override
            public void updateSessionOnReconnection(ServerSession session) {
                ((WebSocketServerSession) session).updateWebSocketSession(wsSession);
            }
        };

        protocolManager.processMessage(messageJson, factory, new ResponseSender() {
            @Override
            public void sendResponse(Message message) throws IOException {

                String jsonMessage = message.toString();
                log.debug("{} <-Res {}", label, jsonMessage);
                sendJsonMessage(jsonMessage);
            }

            @Override
            public void sendPingResponse(Message message) throws IOException {

                String jsonMessage = message.toString();
                log.trace("{} <-Res {}", label, jsonMessage);
                sendJsonMessage(jsonMessage);
            }

            private void sendJsonMessage(String jsonMessage) throws IOException {
                synchronized (wsSession) {
                    if (wsSession.isOpen()) {
                        wsSession.sendMessage(new TextMessage(jsonMessage));
                    } else {
                        log.error("Trying to send a message to a closed session");
                    }
                }
            }
        }, wsSession.getId());

    } catch (Exception e) {
        log.error(label + "Exception processing request", e);
    }

}