Example usage for javax.websocket Session getId

List of usage examples for javax.websocket Session getId

Introduction

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

Prototype

String getId();

Source Link

Document

Provides a unique identifier for the session.

Usage

From source file:feedme.controller.SocketServer.java

/**
 * Method called when a connection is closed
 * *//*from w ww . j  ava  2  s.  c  o m*/
@OnClose
public void onClose(Session session) {

    System.out.println("Session " + session.getId() + " has ended");

    // Getting the client name that exited

    for (Iterator<Map.Entry<String, Session>> it = nameSessionPair.entrySet().iterator(); it.hasNext();) {
        Map.Entry<String, Session> e = it.next();
        if (session.equals(e.getValue())) {
            it.remove();
        }
    }
    // removing the session from sessions list
    sessions.remove(session);

    System.out.println("Sessions size" + sessions.size());
    for (Iterator<Map.Entry<String, Session>> it = nameSessionPair.entrySet().iterator(); it.hasNext();) {
        Map.Entry<String, Session> e = it.next();
        System.out.println("rest id " + e.getKey() + " rest_session_id " + e.getValue().getId().toString());
    }

}

From source file:feedme.controller.SocketServer.java

/**
 * method called when new message received from any client
 * //w  w  w .j a  va  2 s .  com
 * @param message
 *            JSON message from client
 * */
@OnMessage
public void onMessage(String message, Session session) {

    System.out.println("Message from " + session.getId() + ": " + message);

    JSONObject jObj = null;

    // Parsing the json and getting message
    try {
        jObj = new JSONObject(message);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    // Sending the message to all clients
    sendMessageToResturent(jObj);
    //        sendMessageToAll(session.getId(), nameSessionPair.get(session.getId()),
    //                msg, false, false);
}

From source file:feedme.controller.SocketServer.java

/**
 * Called when a socket connection opened
 * *///from   ww w .j  a v  a2  s  .c  om
@OnOpen
public void onOpen(Session session) {

    System.out.println(session.getId() + " has opened a connection");

    Map<String, String> queryParams = getQueryMap(session.getQueryString());

    String name = "";

    if (queryParams.containsKey("name")) {

        // Getting client name via query param
        name = queryParams.get("name");
        try {
            name = URLDecoder.decode(name, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        // Mapping client name and session id

    }
    if (!name.equals("customer")) {
        nameSessionPair.put(name, session);
        sessions.add(session);
    }

    // Adding session to session list

    try {
        // Sending session id to the client that just connected
        session.getBasicRemote().sendText(jsonUtils.getClientDetailsJson(name, "Your session details"));
    } catch (IOException e) {
        e.printStackTrace();
    }

    System.out.println("Sessions size " + sessions.size());
    for (Iterator<Map.Entry<String, Session>> it = nameSessionPair.entrySet().iterator(); it.hasNext();) {
        Map.Entry<String, Session> e = it.next();
        System.out.println("rest id " + e.getKey() + " rest_session_id " + e.getValue().getId().toString());
    }

}

From source file:org.qaclana.services.messagesender.MessageSender.java

@Asynchronous
public void send(@Observes SendMessage event) {
    log.sendingMessageToDestination(event.getDestination().getId(), event.getMessage().getType());

    String messageAsJson;//  w  w  w  . java2  s  .com
    try {
        messageAsJson = JSON_MAPPER.writeValueAsString(event.getMessage());
    } catch (JsonProcessingException e) {
        log.failedToConvertMessageToJson(event.getMessage().toString(), e);
        return;
    }

    Session session = event.getDestination();
    try {
        session.getBasicRemote().sendText(messageAsJson);
    } catch (IOException e) {
        log.failedToSendMessageToDestination(session.getId(), e);
    }
}

From source file:SuperTenantTopicSubscriptionEndpoint.java

@OnOpen
public void onOpen(Session session, @PathParam("topic") String topic,
        @PathParam("adaptorname") String adaptorName) {
    if (log.isDebugEnabled()) {
        log.debug("WebSocket opened, for Session id: " + session.getId() + ", for the Adaptor:" + adaptorName
                + ", for the Topic:" + topic);
    }// ww w  . j a  v a2s. co m
    PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
    carbonContext.setTenantId(-1234);
    tenantId = carbonContext.getTenantId();
    websocketLocalOutputCallbackRegisterService.subscribe(tenantId, adaptorName, topic, session);
}

From source file:SuperTenantDataReceiverEndpoint.java

@OnMessage
public void onMessage(Session session, String message, @PathParam("adaptername") String adapterName) {
    if (log.isDebugEnabled()) {
        log.debug("Received message: " + message + ", for Session id: " + session.getId() + ", for the adapter:"
                + adapterName);//from  ww w.  j av a2s. c  o  m
    }
    PrivilegedCarbonContext.startTenantFlow();
    PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenantId);
    InputEventAdapterListener adapterListener = websocketLocalInputCallbackRegisterService
            .getAdapterListener(adapterName);
    if (adapterListener != null) {
        adapterListener.onEvent(message);
    } else {
        if (log.isDebugEnabled()) {
            log.debug("Dropping the message:" + message + " from session id: " + session.getId()
                    + ", because no input websocket-local adapter exists" + "with name '" + adapterName + "'");
        }
    }
    PrivilegedCarbonContext.endTenantFlow();
}

From source file:cito.server.AbstractEndpoint.java

@OnClose
@Override//from w  ww  . java  2s  . c  o  m
public void onClose(Session session, CloseReason reason) {
    this.log.info("WebSocket connection closed. [id={},principle={},code={},reason={}]", session.getId(),
            session.getUserPrincipal(), reason.getCloseCode(), reason.getReasonPhrase());
    final WebSocketContext ctx = webSocketContext(this.beanManager);
    try (QuietClosable c = ctx.activate(session)) {
        this.registry.unregister(session);
        this.sessionEvent.select(cito.annotation.OnClose.Literal.onClose()).fire(session);
    }
    ctx.dispose(session);
}

From source file:TenantSubscriptionEndpoint.java

@OnOpen
public void onOpen(Session session, @PathParam("adaptorname") String adaptorName,
        @PathParam("tdomain") String tdomain) {
    if (log.isDebugEnabled()) {
        log.debug("WebSocket opened, for Session id: " + session.getId() + ", for tenant domain" + tdomain
                + ", for the Adaptor:" + adaptorName);
    }//w  ww.j  a v a2 s .co  m

    try {
        PrivilegedCarbonContext.startTenantFlow();
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tdomain, true);
        tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
        websocketLocalOutputCallbackRegisterService.subscribe(adaptorName, session);
    } finally {
        PrivilegedCarbonContext.endTenantFlow();
    }
}

From source file:TenantDataReceiverEndpoint.java

@OnOpen
public void onOpen(Session session, @PathParam("tdomain") String tdomain,
        @PathParam("adaptername") String adapterName) {
    if (log.isDebugEnabled()) {
        log.debug("WebSocket opened with session id: " + session.getId() + ", for the tenant domain:" + tdomain
                + ", for the adapter" + adapterName);
    }/*from   w ww.  j ava 2  s . c o  m*/
    PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
    carbonContext.setTenantDomain(tdomain, true);
    tenantId = carbonContext.getTenantId();
}

From source file:TopicSubscriptionEndpoint.java

public void onClose(Session session, CloseReason reason, String topic, String adaptorName, int tenantId) {
    if (log.isDebugEnabled()) {
        log.debug("Closing a WebSocket due to " + reason.getReasonPhrase() + ", for session ID:"
                + session.getId() + ", for request URI - " + session.getRequestURI());
    }/* w w  w. jav a 2  s. c  o m*/
    websocketLocalOutputCallbackRegisterService.unsubscribe(tenantId, adaptorName, topic, session);
}