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

@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);/*  ww w.  j  a v a2s.  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:ch.rasc.s4ws.drawboard.DrawboardWebSocketHandler.java

@SuppressWarnings("resource")
@Selector("sendBinary")
public void consumeSendBinary(Event<ByteBuffer> event) {

    String receiver = event.getHeaders().get("sessionId");
    BinaryMessage binMsg = new BinaryMessage(event.getData());
    if (receiver != null) {
        WebSocketSession session = this.sessions.get(receiver);
        if (session != null) {
            try {
                session.sendMessage(binMsg);
            } catch (IOException e) {
                log.error("sendMessage", e);
            }/*from w w  w  .java 2  s  . c om*/
        }
    } else {
        String excludeId = event.getHeaders().get("excludeId");
        for (WebSocketSession session : this.sessions.values()) {
            if (!session.getId().equals(excludeId)) {
                try {
                    session.sendMessage(binMsg);
                } catch (IOException e) {
                    log.error("sendMessage", e);
                }
            }
        }
    }

}

From source file:ch.rasc.s4ws.drawboard.DrawboardWebSocketHandler.java

@SuppressWarnings("resource")
@Selector("sendString")
public void consumeSendString(Event<String> event) {
    String receiver = event.getHeaders().get("sessionId");
    TextMessage txtMessage = new TextMessage(event.getData());
    if (receiver != null) {
        WebSocketSession session = this.sessions.get(receiver);
        if (session != null) {
            try {
                session.sendMessage(txtMessage);
            } catch (IOException e) {
                log.error("sendMessage", e);
            }//from  ww  w.j a v a 2s.c o  m
        }
    } else {
        String excludeId = event.getHeaders().get("excludeId");
        for (WebSocketSession session : this.sessions.values()) {
            if (!session.getId().equals(excludeId)) {
                try {
                    session.sendMessage(txtMessage);
                } catch (IOException e) {
                    log.error("sendMessage", e);
                }
            }
        }
    }
}

From source file:org.kurento.tutorial.player.PlayerFrameSaverHandler.java

private void start(final WebSocketSession session, JsonObject jsonMessage) {

    String videourl = jsonMessage.get("videourl").getAsString();
    log.info("Video-URL is {} --- Session-ID is {}", videourl, session.getId());

    // create the Media pipeline --- no video source or sink yet, but initial state is PLAYING
    mPipeline = kurento.createMediaPipeline();

    // add FrameSaverVideoFilter to pipeline --- pipeline state changes from PLAYING to READY  
    mFrameSaverProxy = FrameSaverPluginProxy.newInstance(mPipeline);

    // add to pipeline two Media Elements (WebRtcEndpoint, PlayerEndpoint)
    mWebRtc = new WebRtcEndpoint.Builder(mPipeline).build();
    mPlayer = new PlayerEndpoint.Builder(mPipeline, videourl).build();

    // Create and Update User Session
    UserSession user = new UserSession();
    user.setMediaPipeline(mPipeline);// w ww .  j  a v  a 2s. com
    user.setWebRtcEndpoint(mWebRtc);
    user.setPlayerEndpoint(mPlayer);
    users.put(session.getId(), user);

    // Connect The Elements
    mPlayer.connect(mWebRtc);

    // Configure The WebRtcEndpoint --- add ICE-candidate-Found-Listener
    mWebRtc.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());
            }
        }
    });

    String sdpOffer = jsonMessage.get("sdpOffer").getAsString();
    String sdpAnswer = mWebRtc.processOffer(sdpOffer);

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

    // Configure The WebRtcEndpoint --- add Media-State-Change-Listener
    mWebRtc.addMediaStateChangedListener(new EventListener<MediaStateChangedEvent>() {
        @Override
        public void onEvent(MediaStateChangedEvent event) {

            if (event.getNewState() == MediaState.CONNECTED) {
                VideoInfo videoInfo = mPlayer.getVideoInfo();

                JsonObject response = new JsonObject();
                response.addProperty("id", "videoInfo");
                response.addProperty("isSeekable", videoInfo.getIsSeekable());
                response.addProperty("initSeekable", videoInfo.getSeekableInit());
                response.addProperty("endSeekable", videoInfo.getSeekableEnd());
                response.addProperty("videoDuration", videoInfo.getDuration());
                sendMessage(session, response.toString());
            }
        }
    });

    mWebRtc.gatherCandidates();

    // Configure The PlayEndpoint --- add Error-Listener
    mPlayer.addErrorListener(new EventListener<ErrorEvent>() {
        @Override
        public void onEvent(ErrorEvent event) {
            log.info("ErrorEvent: {}", event.getDescription());
            sendPlayEnd(session);
        }
    });

    // Configure The PlayEndpoint --- add End-Of-Stream-Listener
    mPlayer.addEndOfStreamListener(new EventListener<EndOfStreamEvent>() {
        @Override
        public void onEvent(EndOfStreamEvent event) {
            log.info("EndOfStreamEvent: {}", event.getTimestamp());
            sendPlayEnd(session);
        }
    });

    if (mFrameSaverProxy != null) {
        mFrameSaverProxy.setParams(mFrameSaverParams);

        mFrameSaverProxy.setElementsToSplice(mPlayer.getName(), mWebRtc.getName());

        mFrameSaverProxy.setElementsToSplice(mPlayer.getName(), mWebRtc.getName());

        //? mFrameSaverProxy.startPlaying();    // possibly not needed
    }

    // Activate The PlayEndpoint --- start playing
    mPlayer.play();
}

From source file:org.kurento.tutorial.togglevideo.ToggleVideoHandler.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 "processClientOffer":
        processClientOffer(session, jsonMessage);
        break;//from   ww w.ja  v a2  s  .  c om
    case "stop": {
        UserSession user = users.remove(session.getId());
        if (user != null) {
            user.release();
        }
        break;
    }
    case "onIceCandidate": {
        JsonObject jsonCandidate = jsonMessage.get("candidate").getAsJsonObject();

        UserSession user = users.get(session.getId());
        if (user != null) {
            IceCandidate candidate = new IceCandidate(jsonCandidate.get("candidate").getAsString(),
                    jsonCandidate.get("sdpMid").getAsString(), jsonCandidate.get("sdpMLineIndex").getAsInt());
            user.addCandidate(candidate);
        }
        break;
    }
    default:
        sendError(session, "Invalid message with id " + jsonMessage.get("id").getAsString());
        break;
    }
}

From source file:com.zb.app.websocket.server.wrapper.SessionWrapper.java

public SessionWrapper(ClientWrapper clientInfo, WebSocketSession socketSession) {
    if (clientInfo == null) {
        throw new WebSocketException("The WebSocketMessage is Error! ClientKey is null !");
    }/*www  .ja  v  a  2s. co m*/
    this.clientWrapper = clientInfo;
    this.socketSession = socketSession;
    this.registTime = System.currentTimeMillis();
    this.lastHeartbeatTime = System.currentTimeMillis();
    this.id = socketSession.getId();
}

From source file:org.kurento.tutorial.metadata.MetadataHandler.java

private void start(final WebSocketSession session, JsonObject jsonMessage) {
    try {//  ww  w .ja  v  a  2s  .  c o m
        // User session
        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);

        // 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.debug(e.getMessage());
                }
            }
        });

        // Media logic
        KmsDetectFaces detectFaces = new KmsDetectFaces.Builder(pipeline).build();
        KmsShowFaces showFaces = new KmsShowFaces.Builder(pipeline).build();

        webRtcEndpoint.connect(detectFaces);
        detectFaces.connect(showFaces);
        showFaces.connect(webRtcEndpoint);

        // SDP negotiation (offer and answer)
        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()));
        }

        webRtcEndpoint.gatherCandidates();

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

From source file:org.kurento.tutorial.magicmirror.MagicMirrorHandler.java

private void start(final WebSocketSession session, JsonObject jsonMessage) {
    try {/*from  w w  w  .ja  va 2 s  . c o m*/
        // User session
        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);

        // ICE candidates
        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());
                }
            }
        });

        // Media logic
        FaceOverlayFilter faceOverlayFilter = new FaceOverlayFilter.Builder(pipeline).build();

        String appServerUrl = System.getProperty("app.server.url", MagicMirrorApp.DEFAULT_APP_SERVER_URL);
        faceOverlayFilter.setOverlayedImage(appServerUrl + "/img/mario-wings.png", -0.35F, -1.2F, 1.6F, 1.6F);

        webRtcEndpoint.connect(faceOverlayFilter);
        faceOverlayFilter.connect(webRtcEndpoint);

        // SDP negotiation (offer and answer)
        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()));
        }

        webRtcEndpoint.gatherCandidates();

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

From source file:org.kurento.tutorial.showdatachannel.ShowDataChannelHandler.java

private void start(final WebSocketSession session, JsonObject jsonMessage) {
    try {/*from ww w  . j  a va2s  .com*/
        // User session
        UserSession user = new UserSession();
        MediaPipeline pipeline = kurento.createMediaPipeline();
        user.setMediaPipeline(pipeline);
        WebRtcEndpoint webRtcEndpoint = new WebRtcEndpoint.Builder(pipeline).useDataChannels().build();
        user.setWebRtcEndpoint(webRtcEndpoint);
        users.put(session.getId(), user);

        // 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.debug(e.getMessage());
                }
            }
        });

        // Media logic
        KmsShowData kmsShowData = new KmsShowData.Builder(pipeline).build();

        webRtcEndpoint.connect(kmsShowData);
        kmsShowData.connect(webRtcEndpoint);

        // SDP negotiation (offer and answer)
        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()));
        }

        webRtcEndpoint.gatherCandidates();

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

From source file:fi.vtt.nubomedia.armodule.UserSession.java

public String startSession(final WebSocketSession session, String sdpOffer, JsonObject jsonMessage) {
    System.err.println("ME USER SESSION START SESSION");

    // One KurentoClient instance per session
    kurentoClient = KurentoClient.create();
    log.info("Created kurentoClient (session {})", sessionId);

    // Media logic (pipeline and media elements connectivity)
    mediaPipeline = kurentoClient.createMediaPipeline();
    log.info("Created Media Pipeline {} (session {})", mediaPipeline.getId(), session.getId());

    mediaPipeline.setLatencyStats(true);
    log.info("Media Pipeline {} latencyStants set{}", mediaPipeline.getId(), mediaPipeline.getLatencyStats());

    webRtcEndpoint = new WebRtcEndpoint.Builder(mediaPipeline).build();
    handler.createPipeline(this, jsonMessage);

    // WebRTC negotiation
    webRtcEndpoint.addOnIceCandidateListener(new EventListener<OnIceCandidateEvent>() {
        @Override//from   w  ww .ja v a 2  s. co m
        public void onEvent(OnIceCandidateEvent event) {
            JsonObject response = new JsonObject();
            response.addProperty("id", "iceCandidate");
            response.add("candidate", JsonUtils.toJsonObject(event.getCandidate()));
            handler.sendMessage(session, new TextMessage(response.toString()));
        }
    });
    String sdpAnswer = webRtcEndpoint.processOffer(sdpOffer);
    webRtcEndpoint.gatherCandidates();

    return sdpAnswer;
}