Example usage for org.springframework.web.socket TextMessage TextMessage

List of usage examples for org.springframework.web.socket TextMessage TextMessage

Introduction

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

Prototype

public TextMessage(byte[] payload) 

Source Link

Document

Create a new text WebSocket message from the given byte[].

Usage

From source file:eu.nubomedia.tutorial.magicmirror.MagicMirrorHandler.java

private void error(WebSocketSession session, String message) {
    // Send error message to client
    JsonObject response = new JsonObject();
    response.addProperty("id", "error");
    response.addProperty("message", message);
    sendMessage(session, new TextMessage(response.toString()));

    // Release media session
    release(session);//from  ww w .  ja v a2 s  .c o m
}

From source file:org.kurento.tutorial.togglevideo.ToggleVideoHandler.java

private void processClientOffer(final WebSocketSession session, JsonObject jsonMessage) {
    try {//from  w  w w . j a va2s .c  om
        UserSession user = users.get(session.getId());
        if (user == null) {
            log.info("Creating new user session for user");
            MediaPipeline pipeline = kurento.createMediaPipeline();
            user = new UserSession();
            user.setMediaPipeline(pipeline);
            users.put(session.getId(), user);
        }

        WebRtcEndpoint webRtcEndpoint = new WebRtcEndpoint.Builder(user.getMediaPipeline()).build();
        webRtcEndpoint.connect(webRtcEndpoint);
        user.setWebRtcEndpoint(webRtcEndpoint);
        // 3. SDP negotiation
        String sdpOffer = jsonMessage.get("sdpOffer").getAsString();
        String sdpAnswer = user.getWebRtcEndpoint().processOffer(sdpOffer);

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

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

        // 4. Gather ICE candidates
        user.getWebRtcEndpoint().addIceCandidateFoundListener(event -> {
            JsonObject candidateMsg = new JsonObject();
            candidateMsg.addProperty("id", "iceCandidate");
            candidateMsg.add("candidate", JsonUtils.toJsonObject(event.getCandidate()));
            try {
                synchronized (session) {
                    session.sendMessage(new TextMessage(candidateMsg.toString()));
                }
            } catch (IOException e) {
                log.error(e.getMessage());
            }
        });

        user.getWebRtcEndpoint().gatherCandidates();

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

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

private void start(final WebSocketSession session, JsonObject jsonMessage) {
    try {//from  w  w  w  . ja  va2 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:org.acruxsource.sandbox.spring.jmstows.websocket.ChatHandler.java

public void sendMessage(final ChatMessage message) throws IOException {
    logger.info("Sending message to all participants");

    for (WebSocketSession session : sessions) {
        if (session.isOpen()) {
            DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT_PATTERN);
            JsonObjectBuilder jsonObjectBuilder = Json.createObjectBuilder();
            jsonObjectBuilder.add("name", message.getName());
            jsonObjectBuilder.add("message", message.getMessage());
            jsonObjectBuilder.add("date", dateFormat.format(message.getDate()));
            session.sendMessage(new TextMessage(jsonObjectBuilder.build().toString()));
        } else {/*from  w w w . j  av a 2s.c  om*/
            sessions.remove(session);
        }
    }
}

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

private void start(final WebSocketSession session, JsonObject jsonMessage) {
    try {//from w  w w.j  a  v a2 s  . c o m
        // 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:org.kurento.tutorial.metadata.MetadataHandler.java

private void start(final WebSocketSession session, JsonObject jsonMessage) {
    try {//  w  w w .j a  v  a2 s. com
        // 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:ch.rasc.wampspring.testsupport.BaseWampTest.java

protected void authenticate(CompletableFutureWebSocketHandler result, WebSocketSession webSocketSession)
        throws IOException, InterruptedException, InvalidKeyException, NoSuchAlgorithmException,
        ExecutionException, TimeoutException {
    CallMessage authReqCallMessage = new CallMessage("1", "http://api.wamp.ws/procedure#authreq", "a",
            Collections.emptyMap());
    webSocketSession.sendMessage(new TextMessage(authReqCallMessage.toJson(this.jsonFactory)));
    WampMessage response = result.getWampMessage();

    assertThat(response).isInstanceOf(CallResultMessage.class);
    CallResultMessage resultMessage = (CallResultMessage) response;
    assertThat(resultMessage.getCallID()).isEqualTo("1");
    assertThat(resultMessage.getResult()).isNotNull();

    result.reset();//from   ww w.  j a v  a2 s . co m

    String challengeBase64 = (String) resultMessage.getResult();
    String signature = DefaultAuthenticationHandler.generateHMacSHA256("secretofa", challengeBase64);

    CallMessage authCallMessage = new CallMessage("2", "http://api.wamp.ws/procedure#auth", signature);
    webSocketSession.sendMessage(new TextMessage(authCallMessage.toJson(this.jsonFactory)));
    response = result.getWampMessage();

    assertThat(response).isInstanceOf(CallResultMessage.class);
    resultMessage = (CallResultMessage) response;
    assertThat(resultMessage.getCallID()).isEqualTo("2");
    assertThat(resultMessage.getResult()).isNull();

    result.reset();
}

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

private void start(final WebSocketSession session, JsonObject jsonMessage) {
    try {/*w w  w .j  a  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());
                }
            }
        });

        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:eu.nubomedia.tutorial.repository.RepositoryHandler.java

private void play(WebSocketSession session, JsonObject jsonMessage) {
    UserSession user = users.get(session.getId());
    if (user != null) {
        // Media logic for playing
        String sdpOffer = jsonMessage.get("sdpOffer").getAsString();
        String sdpAnswer = user.playRecording(session, sdpOffer);

        // Response message
        JsonObject response = new JsonObject();
        response.addProperty("id", "playResponse");
        response.addProperty("sdpAnswer", sdpAnswer);
        sendMessage(session, new TextMessage(response.toString()));
    }//ww w.ja  va2 s.c  o m
}

From source file:es.urjc.etsii.code.UserSession.java

public void initPresenter(String sdpOffer) {
    log.info("[Session number {} - WS session {}] Init presenter", sessionNumber, wsSession.getId());

    kurentoClient = createKurentoClient();
    mediaPipeline = kurentoClient.createMediaPipeline();
    webRtcEndpoint = createWebRtcEndpoint(mediaPipeline);

    addOnIceCandidateListener();//from  www. j  av  a  2s.  c  o  m

    String sdpAnswer = webRtcEndpoint.processOffer(sdpOffer);
    JsonObject response = new JsonObject();
    response.addProperty("id", "presenterResponse");
    response.addProperty("response", "accepted");
    response.addProperty("sdpAnswer", sdpAnswer);

    handler.sendMessage(wsSession, sessionNumber, new TextMessage(response.toString()));
    webRtcEndpoint.gatherCandidates();

}