Example usage for javax.websocket Session getAsyncRemote

List of usage examples for javax.websocket Session getAsyncRemote

Introduction

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

Prototype

RemoteEndpoint.Async getAsyncRemote();

Source Link

Usage

From source file:org.b3log.symphony.processor.channel.TimelineChannel.java

/**
 * Notifies the specified comment message to browsers.
 *
 * @param message the specified message, for example      <pre>
 * {//from  w ww  .j a  va 2  s  . co  m
 *     "commentContent": ""
 * }
 * </pre>
 */
public static void notifyTimeline(final JSONObject message) {
    final String msgStr = message.toString();

    synchronized (SESSIONS) {
        for (final Session session : SESSIONS) {
            if (session.isOpen()) {
                session.getAsyncRemote().sendText(msgStr);
            }
        }
    }
}

From source file:org.wso2.carbon.device.mgt.iot.digitaldisplay.service.impl.websocket.DigitalDisplayWebSocketServerEndPoint.java

/**
 * This method will be invoked when a message received from device
 * to send client./*from   ww  w  .j av  a 2s .  c om*/
 *
 * @param sessionId   the client of message to be sent.
 * @param message the message sent by device to client
 */
public static void sendMessage(String sessionId, StringBuilder message) {
    Session session = clientSessions.get(sessionId);
    if (session != null) {
        session.getAsyncRemote().sendText(message.toString());
    } else {
        log.error("Client already disconnected.");
    }
}

From source file:org.wso2.carbon.event.output.adaptor.websocket.WebsocketEventAdaptor.java

@Override
protected void publish(OutputEventAdaptorMessageConfiguration outputEventAdaptorMessageConfiguration,
        Object message, OutputEventAdaptorConfiguration outputEventAdaptorConfiguration, int tenantId) {
    String topic = outputEventAdaptorMessageConfiguration.getOutputMessageProperties()
            .get(WebsocketEventAdaptorConstants.ADAPTER_TOPIC);
    String socketServerUrl = outputEventAdaptorConfiguration.getOutputProperties()
            .get(WebsocketEventAdaptorConstants.ADAPTER_SERVER_URL);
    if (!socketServerUrl.startsWith("ws://")) {
        throw new OutputEventAdaptorEventProcessingException(
                "Provided websocket URL - " + socketServerUrl + " is invalid.");
    }/*ww w.j  a  va2s  . co m*/
    if (topic != null) {
        socketServerUrl = socketServerUrl + "/" + topic;
    }
    ConcurrentHashMap<String, Session> urlSessionMap = outputEventAdaptorSessionMap.get(tenantId);
    if (urlSessionMap == null) {
        urlSessionMap = new ConcurrentHashMap<String, Session>();
        if (null != outputEventAdaptorSessionMap.putIfAbsent(tenantId, urlSessionMap)) {
            urlSessionMap = outputEventAdaptorSessionMap.get(tenantId);
        }
    }
    Session session = urlSessionMap.get(socketServerUrl);
    if (session == null) { //TODO: Handle reconnecting, in case server disconnects. Suggestion: Create a scheduler.
        ClientEndpointConfig cec = ClientEndpointConfig.Builder.create().build();
        ClientManager client = ClientManager.createClient();
        try {
            session = client.connectToServer(new WebsocketClient(), cec, new URI(socketServerUrl));
            if (null != urlSessionMap.putIfAbsent(socketServerUrl, session)) {
                session.close();
                session = urlSessionMap.get(socketServerUrl);
            }
        } catch (DeploymentException e) {
            throw new OutputEventAdaptorEventProcessingException(e);
        } catch (IOException e) {
            throw new OutputEventAdaptorEventProcessingException(e);
        } catch (URISyntaxException e) {
            throw new OutputEventAdaptorEventProcessingException(e);
        }
    }
    session.getAsyncRemote().sendText(message.toString());
}