Example usage for javax.websocket Session getBasicRemote

List of usage examples for javax.websocket Session getBasicRemote

Introduction

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

Prototype

RemoteEndpoint.Basic getBasicRemote();

Source Link

Usage

From source file:org.wso2.carbon.identity.agent.outbound.server.SessionHandler.java

/**
 * Remove websocket session from cache and kill sessions.
 * @param tenantDomain Tenant domain/*from   w w w .j  a  va 2  s .c  o m*/
 * @param userstoreDomain User store domain
 * @throws IOException
 */
public void removeAndKillSessions(String tenantDomain, String userstoreDomain)
        throws IOException, JSONException {
    List<Session> sessionList = sessions.get(getKey(tenantDomain, userstoreDomain));
    if (sessionList != null) {
        for (Session session : sessionList) {
            UserOperation userOperation = new UserOperation();
            userOperation.setRequestType(UserStoreConstants.UM_OPERATION_TYPE_ERROR);
            JSONObject jsonMessage = new JSONObject();
            jsonMessage.put("message", "Closing client connection from server.");
            userOperation.setRequestData(jsonMessage.toString());
            session.getBasicRemote().sendText(MessageRequestUtil.getUserOperationJSONMessage(userOperation));
        }
        sessions.remove(getKey(tenantDomain, userstoreDomain));
    }
}

From source file:org.wso2.carbon.identity.agent.outbound.server.UserStoreServerEndpoint.java

/**
 * Send error message to client/*from  ww w  . j  a  v a 2s  .  c o  m*/
 * @param session web socket session
 * @param message Error message
 * @throws IOException
 */
private void sendErrorMessage(Session session, String message) throws IOException, JSONException {
    LOGGER.error(message);
    UserOperation userOperation = new UserOperation();
    userOperation.setRequestType(UserStoreConstants.UM_OPERATION_TYPE_ERROR);
    JSONObject jsonMessage = new JSONObject();
    jsonMessage.put("message", message);
    userOperation.setRequestData(jsonMessage.toString());
    session.getBasicRemote().sendText(MessageRequestUtil.getUserOperationJSONMessage(userOperation));
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        LOGGER.error("Error occurred while sleep before close session");
    }
    session.close();
}

From source file:org.wso2.carbon.integration.test.client.WebSocketClient.java

public void send(String url, String message) {
    receivedMessage = null;/*  w  w  w  .  jav a 2  s .co  m*/
    URI uri = URI.create(url);

    try {
        WebSocketContainer container = ContainerProvider.getWebSocketContainer();
        try {
            // Attempt Connect
            Session session = container.connectToServer(EventSocketClient.class, uri);
            // Send a message
            session.getBasicRemote().sendText(message);
            // Close session
            session.close();
        } finally {
            // Force lifecycle stop when done with container.
            // This is to free up threads and resources that the
            // JSR-356 container allocates. But unfortunately
            // the JSR-356 spec does not handle lifecycles (yet)
            if (container instanceof LifeCycle) {
                ((LifeCycle) container).stop();
            }
        }
    } catch (Throwable t) {
        log.error(t);
    }
}

From source file:servlets.WSChat.java

@OnMessage
public String onMessage(String message) {
    synchronized (sessions) {
        for (Session session : sessions) {
            try {
                if (!message.isEmpty()) {
                    if (message.length() > 30) {
                        String chaine = "";
                        String[] split = message.split("");
                        for (int i = 0; i < 30; i++) {
                            chaine += split[i];
                        }/*from  www. j a v a  2s  . c  om*/
                        message = chaine;
                    }

                    /*
                    String chaine = "";
                    String[] split = message.split("");
                    for(int i=0 ; i < message.length() ; i++) {                            
                    if ((i > 0) && (split[i].equals(split[i-1]))) {
                        if (message.length() > i+1) {
                            if (split[i].equals(split[i+1])) {
                                if ((i == 87) && (i == 174)) {
                                    split[i] = " ";
                                }
                                // TODO : inserer un espace entre i et i+1
                            }
                        }
                    }
                    chaine += split[i];
                    }
                    */
                    session.getBasicRemote().sendText(escapeHtml4(message));
                }
            } catch (IOException ex) {
                Logger.getLogger(WSChat.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    return null;
}