Example usage for org.springframework.web.socket WebSocketSession getId

List of usage examples for org.springframework.web.socket WebSocketSession getId

Introduction

In this page you can find the example usage for org.springframework.web.socket WebSocketSession getId.

Prototype

String getId();

Source Link

Document

Return a unique session identifier.

Usage

From source file:org.kurento.tutorial.chroma.ChromaHandler.java

private void start(final WebSocketSession session, JsonObject jsonMessage) {
    try {//  w  w  w . ja v  a 2  s .  c o  m
        // Media Logic (Media Pipeline and Elements)
        UserSession user = new UserSession();
        MediaPipeline pipeline = kurento.createMediaPipeline();
        user.setMediaPipeline(pipeline);
        WebRtcEndpoint webRtcEndpoint = new WebRtcEndpoint.Builder(pipeline).build();
        user.setWebRtcEndpoint(webRtcEndpoint);
        users.put(session.getId(), user);

        webRtcEndpoint.addIceCandidateFoundListener(new EventListener<IceCandidateFoundEvent>() {

            @Override
            public void onEvent(IceCandidateFoundEvent event) {
                JsonObject response = new JsonObject();
                response.addProperty("id", "iceCandidate");
                response.add("candidate", JsonUtils.toJsonObject(event.getCandidate()));
                try {
                    synchronized (session) {
                        session.sendMessage(new TextMessage(response.toString()));
                    }
                } catch (IOException e) {
                    log.debug(e.getMessage());
                }
            }
        });

        ChromaFilter chromaFilter = new ChromaFilter.Builder(pipeline, new WindowParam(5, 5, 40, 40)).build();
        String appServerUrl = System.getProperty("app.server.url", ChromaApp.DEFAULT_APP_SERVER_URL);
        chromaFilter.setBackground(appServerUrl + "/img/mario.jpg");

        webRtcEndpoint.connect(chromaFilter);
        chromaFilter.connect(webRtcEndpoint);

        // SDP negotiation (offer and answer)
        String sdpOffer = jsonMessage.get("sdpOffer").getAsString();
        String sdpAnswer = webRtcEndpoint.processOffer(sdpOffer);

        // Sending response back to client
        JsonObject response = new JsonObject();
        response.addProperty("id", "startResponse");
        response.addProperty("sdpAnswer", sdpAnswer);

        synchronized (session) {
            session.sendMessage(new TextMessage(response.toString()));
        }
        webRtcEndpoint.gatherCandidates();

    } catch (Throwable t) {
        sendError(session, t.getMessage());
    }
}

From source file:com.music.web.websocket.GameHandler.java

@Override
protected void handleTextMessage(WebSocketSession session, TextMessage textMessage) throws Exception {
    try {/* ww w  .  j  a  v a 2s. c o m*/
        GameMessage message = getMessage(textMessage);
        switch (message.getAction()) {
        case INITIALIZE:
            initialize(message, session);
            break;
        case JOIN:
            join(message.getGameId(), message.getPlayerName(), session);
            break;
        case LEAVE:
            leave(session.getId());
            break;
        case START:
            startGame(message);
            break;
        case ANSWER:
            answer(message, session.getId());
            break;
        case JOIN_RANDOM:
            joinRandomGame(message.getPlayerName(), session);
            break;
        }
    } catch (Exception ex) {
        logger.error("Exception occurred while handling message", ex);
    }
}

From source file:org.kurento.tutorial.crowddetector.CrowdDetectorHandler.java

@Override
public void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
    JsonObject jsonMessage = gson.fromJson(message.getPayload(), JsonObject.class);

    log.debug("Incoming message: {}", jsonMessage);

    switch (jsonMessage.get("id").getAsString()) {
    case "start":
        start(session, jsonMessage);// w  ww. j  av  a 2s.co m
        break;

    case "stop": {
        UserSession user = users.remove(session.getId());
        if (user != null) {
            user.release();
        }
        break;
    }

    case "onIceCandidate": {
        JsonObject candidate = jsonMessage.get("candidate").getAsJsonObject();

        UserSession user = users.get(session.getId());
        if (user != null) {
            IceCandidate cand = new IceCandidate(candidate.get("candidate").getAsString(),
                    candidate.get("sdpMid").getAsString(), candidate.get("sdpMLineIndex").getAsInt());
            user.addCandidate(cand);
        }
        break;
    }

    default:
        sendError(session, "Invalid message with id " + jsonMessage.get("id").getAsString());
        break;
    }
}

From source file:org.kurento.tutorial.platedetector.PlateDetectorHandler.java

private void start(final WebSocketSession session, JsonObject jsonMessage) {
    try {/*from w w  w.j ava2 s .c o m*/
        // Media Logic (Media Pipeline and Elements)
        UserSession user = new UserSession();
        MediaPipeline pipeline = kurento.createMediaPipeline();
        user.setMediaPipeline(pipeline);
        WebRtcEndpoint webRtcEndpoint = new WebRtcEndpoint.Builder(pipeline).build();
        user.setWebRtcEndpoint(webRtcEndpoint);
        users.put(session.getId(), user);

        webRtcEndpoint.addIceCandidateFoundListener(new EventListener<IceCandidateFoundEvent>() {

            @Override
            public void onEvent(IceCandidateFoundEvent event) {
                JsonObject response = new JsonObject();
                response.addProperty("id", "iceCandidate");
                response.add("candidate", JsonUtils.toJsonObject(event.getCandidate()));
                try {
                    synchronized (session) {
                        session.sendMessage(new TextMessage(response.toString()));
                    }
                } catch (IOException e) {
                    log.debug(e.getMessage());
                }
            }
        });

        PlateDetectorFilter plateDetectorFilter = new PlateDetectorFilter.Builder(pipeline).build();

        webRtcEndpoint.connect(plateDetectorFilter);
        plateDetectorFilter.connect(webRtcEndpoint);

        plateDetectorFilter.addPlateDetectedListener(new EventListener<PlateDetectedEvent>() {
            @Override
            public void onEvent(PlateDetectedEvent event) {
                JsonObject response = new JsonObject();
                response.addProperty("id", "plateDetected");
                response.addProperty("plate", event.getPlate());
                try {
                    session.sendMessage(new TextMessage(response.toString()));
                } catch (Throwable t) {
                    sendError(session, t.getMessage());
                }
            }
        });

        // SDP negotiation (offer and answer)
        String sdpOffer = jsonMessage.get("sdpOffer").getAsString();
        String sdpAnswer = webRtcEndpoint.processOffer(sdpOffer);

        // Sending response back to client
        JsonObject response = new JsonObject();
        response.addProperty("id", "startResponse");
        response.addProperty("sdpAnswer", sdpAnswer);

        synchronized (session) {
            session.sendMessage(new TextMessage(response.toString()));
        }
        webRtcEndpoint.gatherCandidates();
    } catch (Throwable t) {
        sendError(session, t.getMessage());
    }
}

From source file:com.github.mrstampy.gameboot.websocket.WebSocketSessionRegistry.java

private boolean sessionCheck(String groupName, WebSocketSession wss) {
    boolean open = wss.isOpen();

    if (!open) {//www.ja v  a 2  s. c  om
        log.warn("Session {} is closed in group {}, cannot send message", wss.getId(), groupName);
        removeFromGroup(groupName, wss);
    }

    return open;
}

From source file:ch.rasc.wampspring.session.WebSocketRegistryListener.java

private void registerWsSession(WebSocketSession wsSession) {
    String httpSessionId = (String) wsSession.getAttributes().get(SessionSupport.SPRING_SESSION_ID_ATTR_NAME);
    if (httpSessionId == null) {
        return;/*from   www  .j  av a 2 s  .co  m*/
    }

    Map<String, WebSocketSession> sessions = this.httpSessionIdToWsSessions.get(httpSessionId);
    if (sessions == null) {
        sessions = new ConcurrentHashMap<>();
        this.httpSessionIdToWsSessions.putIfAbsent(httpSessionId, sessions);
        sessions = this.httpSessionIdToWsSessions.get(httpSessionId);
    }
    sessions.put(wsSession.getId(), wsSession);
}

From source file:org.kurento.tutorial.frame_saver.FrameSaverHandler.java

@Override
public void handleTextMessage(WebSocketSession aSession, TextMessage aMessage) throws Exception {
    JsonObject json_obj_msg = s_Gson.fromJson(aMessage.getPayload(), JsonObject.class);

    mLogger.debug("Incoming message: {}", json_obj_msg);

    String msg_id = json_obj_msg.get("id").getAsString();

    switch (msg_id) {
    case "start":
        start(aSession, json_obj_msg);//from  w  ww.  j a va 2 s .  co m
        break;

    case "stop": {
        UserSession user_session = mSessionsMap.remove(aSession.getId());

        if (user_session != null) {
            user_session.release();
        }
        break;
    }

    case "onIceCandidate": {
        UserSession user_session = mSessionsMap.get(aSession.getId());

        if (user_session != null) {
            JsonObject old_candidate = json_obj_msg.get("candidate").getAsJsonObject();

            IceCandidate new_candidate = new IceCandidate(old_candidate.get("candidate").getAsString(),
                    old_candidate.get("sdpMid").getAsString(), old_candidate.get("sdpMLineIndex").getAsInt());
            user_session.addCandidate(new_candidate);
        }
        break;
    }

    default:
        sendError(aSession, "Invalid JSON message id: " + msg_id);
        break;
    }
}

From source file:org.kurento.tutorial.helloworld.HelloWorldHandler.java

private void start(final WebSocketSession session, JsonObject jsonMessage) {
    try {/* w w w  .j  a v  a 2s  .co  m*/
        // 1. Media logic (webRtcEndpoint in loopback)
        MediaPipeline pipeline = kurento.createMediaPipeline();
        WebRtcEndpoint webRtcEndpoint = new WebRtcEndpoint.Builder(pipeline).build();
        webRtcEndpoint.connect(webRtcEndpoint);

        // 2. Store user session
        UserSession user = new UserSession();
        user.setMediaPipeline(pipeline);
        user.setWebRtcEndpoint(webRtcEndpoint);
        users.put(session.getId(), user);

        // 3. SDP negotiation
        String sdpOffer = jsonMessage.get("sdpOffer").getAsString();
        String sdpAnswer = webRtcEndpoint.processOffer(sdpOffer);

        JsonObject response = new JsonObject();
        response.addProperty("id", "startResponse");
        response.addProperty("sdpAnswer", sdpAnswer);

        synchronized (session) {
            session.sendMessage(new TextMessage(response.toString()));
        }

        // 4. Gather ICE candidates
        webRtcEndpoint.addOnIceCandidateListener(new EventListener<OnIceCandidateEvent>() {
            @Override
            public void onEvent(OnIceCandidateEvent event) {
                JsonObject response = new JsonObject();
                response.addProperty("id", "iceCandidate");
                response.add("candidate", JsonUtils.toJsonObject(event.getCandidate()));
                try {
                    synchronized (session) {
                        session.sendMessage(new TextMessage(response.toString()));
                    }
                } catch (IOException e) {
                    log.error(e.getMessage());
                }
            }
        });
        webRtcEndpoint.gatherCandidates();

    } catch (Throwable t) {
        sendError(session, t.getMessage());
    }
}

From source file:com.github.mrstampy.gameboot.websocket.WebSocketSessionRegistry.java

private void sendText(String groupName, WebSocketSession wss, String message) {
    TextMessage bm = new TextMessage(message);
    try {/*from   w w  w .j  a va  2 s.  co m*/
        wss.sendMessage(bm);
        log.debug("Sent message to web socket session {} in group {}", wss.getId(), groupName);
    } catch (IOException e) {
        log.error("Unexpected exception sending message to web socket session {}", wss.getId(), e);
    }
}

From source file:org.kurento.tutorial.frame_saver.FrameSaverHandler.java

private void start(final WebSocketSession aSession, JsonObject aJsonMsg) {
    try {// w w w  .ja v  a2 s.  com
        MediaPipeline new_pipeline = mClientKurento.createMediaPipeline();

        WebRtcEndpoint new_endpoint = new WebRtcEndpoint.Builder(new_pipeline).build();

        UserSession new_session = new UserSession();

        new_session.setMediaPipeline(new_pipeline);

        new_session.setWebRtcEndpoint(new_endpoint);

        mSessionsMap.put(aSession.getId(), new_session);

        new_endpoint.addIceCandidateFoundListener(new EventListener<IceCandidateFoundEvent>() {
            @Override
            public void onEvent(IceCandidateFoundEvent event) {
                JsonObject response_obj = new JsonObject();

                response_obj.addProperty("id", "iceCandidate");

                response_obj.add("candidate", JsonUtils.toJsonObject(event.getCandidate()));

                String response_str = response_obj.toString();

                try {
                    synchronized (aSession) {
                        aSession.sendMessage(new TextMessage(response_str));
                    }
                } catch (IOException ex) {
                    mLogger.debug(ex.getMessage());
                }
            }
        });

        FrameSaverFilter frame_saver_filter = new FrameSaverFilter.Builder(new_pipeline).build();

        new_endpoint.connect(frame_saver_filter);

        frame_saver_filter.connect(new_endpoint);

        frame_saver_filter.addFrameSaverListener(new EventListener<FrameSaverEvent>() {
            @Override
            public void onEvent(FrameSaverEvent event) {
                JsonObject response_obj = new JsonObject();

                response_obj.addProperty("id", "TODO");

                String response_str = response_obj.toString();

                try {
                    aSession.sendMessage(new TextMessage(response_str));
                } catch (Throwable ex) {
                    sendError(aSession, ex.getMessage());
                }
            }
        });

        // SDP negotiation (offer and answer)
        String sdp_offer = aJsonMsg.get("sdpOffer").getAsString();

        String sdp_reply = new_endpoint.processOffer(sdp_offer);

        // Sending response back to client
        JsonObject response_obj = new JsonObject();
        response_obj.addProperty("id", "startResponse");
        response_obj.addProperty("sdpAnswer", sdp_reply);

        String response_str = response_obj.toString();

        synchronized (aSession) {
            aSession.sendMessage(new TextMessage(response_str));
        }

        new_endpoint.gatherCandidates();
    } catch (Throwable ex) {
        sendError(aSession, ex.getMessage());
    }
}