Example usage for javax.websocket Session addMessageHandler

List of usage examples for javax.websocket Session addMessageHandler

Introduction

In this page you can find the example usage for javax.websocket Session addMessageHandler.

Prototype

void addMessageHandler(MessageHandler handler) throws IllegalStateException;

Source Link

Document

Registers a MessageHandler for incoming messages.

Usage

From source file:org.springframework.samples.websocket.client.SimpleClientEndpoint.java

@Override
public void onOpen(final Session session, EndpointConfig config) {
    try {// w w  w .  java 2  s.c om
        String message = this.greetingService.getGreeting();
        session.getBasicRemote().sendText(message);
    } catch (IOException e) {
        e.printStackTrace();
    }
    session.addMessageHandler(new MessageHandler.Whole<String>() {
        @Override
        public void onMessage(String message) {
            logger.debug("Received message: " + message);
            try {
                session.close();
                logger.debug("Closed session");
            } catch (IOException e) {
                logger.error("Failed to close", e);
            }
        }
    });
}

From source file:org.springframework.web.socket.adapter.standard.StandardWebSocketHandlerAdapter.java

@Override
public void onOpen(final javax.websocket.Session session, EndpointConfig config) {
    this.wsSession.initializeNativeSession(session);

    if (this.handler.supportsPartialMessages()) {
        session.addMessageHandler(new MessageHandler.Partial<String>() {
            @Override/*www  .  j a v a  2s.c om*/
            public void onMessage(String message, boolean isLast) {
                handleTextMessage(session, message, isLast);
            }
        });
        session.addMessageHandler(new MessageHandler.Partial<ByteBuffer>() {
            @Override
            public void onMessage(ByteBuffer message, boolean isLast) {
                handleBinaryMessage(session, message, isLast);
            }
        });
    } else {
        session.addMessageHandler(new MessageHandler.Whole<String>() {
            @Override
            public void onMessage(String message) {
                handleTextMessage(session, message, true);
            }
        });
        session.addMessageHandler(new MessageHandler.Whole<ByteBuffer>() {
            @Override
            public void onMessage(ByteBuffer message) {
                handleBinaryMessage(session, message, true);
            }
        });
    }

    session.addMessageHandler(new MessageHandler.Whole<javax.websocket.PongMessage>() {
        @Override
        public void onMessage(javax.websocket.PongMessage message) {
            handlePongMessage(session, message.getApplicationData());
        }
    });

    try {
        this.handler.afterConnectionEstablished(this.wsSession);
    } catch (Throwable ex) {
        ExceptionWebSocketHandlerDecorator.tryCloseWithError(this.wsSession, ex, logger);
    }
}

From source file:org.springframework.web.socket.adapter.StandardEndpointAdapter.java

@Override
public void onOpen(final javax.websocket.Session session, EndpointConfig config) {

    this.wsSession.initSession(session);

    if (this.handler.supportsPartialMessages()) {
        session.addMessageHandler(new MessageHandler.Partial<String>() {
            @Override/*from ww w  .  j  a  v a2s .  c o  m*/
            public void onMessage(String message, boolean isLast) {
                handleTextMessage(session, message, isLast);
            }
        });
        session.addMessageHandler(new MessageHandler.Partial<ByteBuffer>() {
            @Override
            public void onMessage(ByteBuffer message, boolean isLast) {
                handleBinaryMessage(session, message, isLast);
            }
        });
    } else {
        session.addMessageHandler(new MessageHandler.Whole<String>() {
            @Override
            public void onMessage(String message) {
                handleTextMessage(session, message, true);
            }
        });
        session.addMessageHandler(new MessageHandler.Whole<ByteBuffer>() {
            @Override
            public void onMessage(ByteBuffer message) {
                handleBinaryMessage(session, message, true);
            }
        });
    }

    try {
        this.handler.afterConnectionEstablished(this.wsSession);
    } catch (Throwable t) {
        ExceptionWebSocketHandlerDecorator.tryCloseWithError(this.wsSession, t, logger);
        return;
    }
}

From source file:org.springframework.web.socket.adapter.StandardWebSocketHandlerAdapter.java

@Override
public void onOpen(final javax.websocket.Session session, EndpointConfig config) {

    this.wsSession.initializeNativeSession(session);

    if (this.handler.supportsPartialMessages()) {
        session.addMessageHandler(new MessageHandler.Partial<String>() {
            @Override//from   ww w.  jav  a 2  s .  c o m
            public void onMessage(String message, boolean isLast) {
                handleTextMessage(session, message, isLast);
            }
        });
        session.addMessageHandler(new MessageHandler.Partial<ByteBuffer>() {
            @Override
            public void onMessage(ByteBuffer message, boolean isLast) {
                handleBinaryMessage(session, message, isLast);
            }
        });
    } else {
        session.addMessageHandler(new MessageHandler.Whole<String>() {
            @Override
            public void onMessage(String message) {
                handleTextMessage(session, message, true);
            }
        });
        session.addMessageHandler(new MessageHandler.Whole<ByteBuffer>() {
            @Override
            public void onMessage(ByteBuffer message) {
                handleBinaryMessage(session, message, true);
            }
        });
    }

    session.addMessageHandler(new MessageHandler.Whole<javax.websocket.PongMessage>() {
        @Override
        public void onMessage(javax.websocket.PongMessage message) {
            handlePongMessage(session, message.getApplicationData());
        }
    });

    try {
        this.handler.afterConnectionEstablished(this.wsSession);
    } catch (Throwable t) {
        ExceptionWebSocketHandlerDecorator.tryCloseWithError(this.wsSession, t, logger);
        return;
    }
}

From source file:org.wso2.carbon.event.input.adapter.websocket.internal.WebsocketClient.java

@Override
public void onOpen(Session session, EndpointConfig endpointConfig) {
    session.addMessageHandler(new MessageHandler.Whole<String>() {
        @Override/*from  w ww.  ja v a 2s  . c  o m*/
        public void onMessage(String message) {
            PrivilegedCarbonContext.startTenantFlow();
            PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenantId);
            if (log.isDebugEnabled()) {
                log.debug("Received message: '" + message);
            }
            inputEventAdapterListener.onEvent(message);
            PrivilegedCarbonContext.endTenantFlow();
        }
    });
}

From source file:org.wso2.carbon.event.input.adaptor.websocket.internal.WebsocketClient.java

@Override
public void onOpen(Session session, EndpointConfig EndpointConfig) {
    session.addMessageHandler(new MessageHandler.Whole<String>() {
        @Override//  www .  ja v  a  2  s.c  om
        public void onMessage(String message) {
            if (log.isDebugEnabled()) {
                log.debug("Received message: " + message);
            }
            inputEventAdaptorListener.onEventCall(message);
        }
    });
}

From source file:pt.webdetails.cda.push.CdaPushQueryEndpoint.java

/**
 * This method is called whenever a client creates a new websocket connection to the URL registered to
 * this endpoint.//from  ww  w  .j  a  v  a 2  s . c om
 *
 * @param session the websocket session.
 * @param endpointConfig the endpoint configuration.
 */
@Override
public void onOpen(Session session, EndpointConfig endpointConfig) {
    PentahoRequestContextHolder.setRequestContext(() -> (String) endpointConfig.getUserProperties()
            .get(WebsocketEndpointConfig.getInstanceToReadProperties().getServletContextPathPropertyName()));
    IPentahoRequestContext requestContext = PentahoRequestContextHolder.getRequestContext();

    this.queryHandler = new CdaPushQueryMessageHandler(session, requestContext);
    this.queryHandler.getWebsocketJsonQueryEndpoint().onOpen(null);

    session.setMaxTextMessageBufferSize(Integer.parseInt(endpointConfig.getUserProperties()
            .get(WebsocketEndpointConfig.getInstanceToReadProperties().getMaxMessagePropertyName())
            .toString()));
    session.setMaxBinaryMessageBufferSize(Integer.parseInt(endpointConfig.getUserProperties()
            .get(WebsocketEndpointConfig.getInstanceToReadProperties().getMaxMessagePropertyName())
            .toString()));

    session.addMessageHandler(queryHandler);
}