From a86f9de2cd32a333db11b49e90bda0541411b341 Mon Sep 17 00:00:00 2001 From: Felipe Santiago Corro Date: Tue, 1 May 2018 08:14:11 -0300 Subject: [PATCH] Spring Web Socket - send to user (#4135) --- spring-mvc-java/pom.xml | 7 ++++ .../web/config/WebSocketSendToUserConfig.java | 42 +++++++++++++++++++ .../WebsocketSendToUserController.java | 34 +++++++++++++++ .../resources/js/webSocketSendToUserApp.js | 24 +++++++++++ 4 files changed, 107 insertions(+) create mode 100644 spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebSocketSendToUserConfig.java create mode 100644 spring-mvc-java/src/main/java/com/baeldung/web/controller/WebsocketSendToUserController.java create mode 100644 spring-mvc-java/src/main/webapp/resources/js/webSocketSendToUserApp.js diff --git a/spring-mvc-java/pom.xml b/spring-mvc-java/pom.xml index 9a4dc4870d..f8d1d32f63 100644 --- a/spring-mvc-java/pom.xml +++ b/spring-mvc-java/pom.xml @@ -148,6 +148,13 @@ javax.el 2.2.4 + + + + + com.google.code.gson + gson + diff --git a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebSocketSendToUserConfig.java b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebSocketSendToUserConfig.java new file mode 100644 index 0000000000..7f14380e5e --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebSocketSendToUserConfig.java @@ -0,0 +1,42 @@ +package com.baeldung.spring.web.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.http.server.ServerHttpRequest; +import org.springframework.http.server.ServerHttpResponse; +import org.springframework.http.server.ServletServerHttpRequest; +import org.springframework.messaging.simp.config.MessageBrokerRegistry; +import org.springframework.web.socket.WebSocketHandler; +import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer; +import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; +import org.springframework.web.socket.config.annotation.StompEndpointRegistry; +import org.springframework.web.socket.server.support.DefaultHandshakeHandler; + +import javax.servlet.http.HttpSession; +import java.util.Map; + +@Configuration +@EnableWebSocketMessageBroker +public class WebSocketSendToUserConfig extends AbstractWebSocketMessageBrokerConfigurer { + + @Override + public void configureMessageBroker(MessageBrokerRegistry config) { + config.enableSimpleBroker("/topic/", "/queue/"); + config.setApplicationDestinationPrefixes("/app"); + } + + @Override + public void registerStompEndpoints(StompEndpointRegistry registry) { + registry.addEndpoint("/greeting").setHandshakeHandler(new DefaultHandshakeHandler() { + + //Get sessionId from request and set it in Map attributes + public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, + Map attributes) throws Exception { + if (request instanceof ServletServerHttpRequest) { + ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) request; + HttpSession session = servletRequest.getServletRequest().getSession(); + attributes.put("sessionId", session.getId()); + } + return true; + }}).withSockJS(); + } +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/web/controller/WebsocketSendToUserController.java b/spring-mvc-java/src/main/java/com/baeldung/web/controller/WebsocketSendToUserController.java new file mode 100644 index 0000000000..d4c15aead9 --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/web/controller/WebsocketSendToUserController.java @@ -0,0 +1,34 @@ +package com.baeldung.web.controller; + +import com.google.gson.Gson; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.messaging.handler.annotation.MessageExceptionHandler; +import org.springframework.messaging.handler.annotation.MessageMapping; +import org.springframework.messaging.handler.annotation.Payload; +import org.springframework.messaging.simp.SimpMessageSendingOperations; +import org.springframework.messaging.simp.annotation.SendToUser; +import org.springframework.stereotype.Controller; + +import java.security.Principal; +import java.util.Map; + +@Controller +public class WebsocketSendToUserController { + + @Autowired + private SimpMessageSendingOperations messagingTemplate; + + private Gson gson = new Gson(); + + @MessageMapping("/message") + @SendToUser("/queue/reply") + public String processMessageFromClient(@Payload String message, Principal principal) throws Exception { + return gson.fromJson(message, Map.class).get("name").toString(); + } + + @MessageExceptionHandler + @SendToUser("/queue/errors") + public String handleException(Throwable exception) { + return exception.getMessage(); + } +} diff --git a/spring-mvc-java/src/main/webapp/resources/js/webSocketSendToUserApp.js b/spring-mvc-java/src/main/webapp/resources/js/webSocketSendToUserApp.js new file mode 100644 index 0000000000..5baa28a11d --- /dev/null +++ b/spring-mvc-java/src/main/webapp/resources/js/webSocketSendToUserApp.js @@ -0,0 +1,24 @@ +function connect() { + var socket = new WebSocket('ws://localhost:8080/greeting'); + ws = Stomp.over(socket); + + ws.connect({}, function(frame) { + ws.subscribe("/user/queue/errors", function(message) { + alert("Error " + message.body); + }); + + ws.subscribe("/user/queue/reply", function(message) { + alert("Message " + message.body); + }); + }, function(error) { + alert("STOMP error " + error); + }); +} + +function disconnect() { + if (ws != null) { + ws.close(); + } + setConnected(false); + console.log("Disconnected"); +}