[英]Websocket with Spring mvc, stomp, sockjs, angular JS
With the example provided by spring.io and http://www.baeldung.com/websockets-spring is helped to create a websocket connection between client and server, but my case is. 通过spring.io和http://www.baeldung.com/websockets-spring提供的示例,可以帮助在客户端和服务器之间创建websocket连接,但我的情况是这样。 - Some one is creating message from UI that is passed to Spring controller (Separate controller). -有人正在通过UI创建消息,该消息传递给Spring控制器(单独的控制器)。 - From this controller I need to notify/send/broadcast this message to all connected clients. -我需要从此控制器通知/发送/广播此消息给所有连接的客户端。 - How the message is passed to handler from controller where message is received. -如何将消息从接收消息的控制器传递到处理程序。 I also refereed WebSocket with Sockjs & Spring 4 but without Stomp here and the same question is posted. 我还引用了带有Sockjs和Spring 4的WebSocket,但此处没有Stomp ,并且发布了相同的问题。
Can some one help me here, Thanks in advance !! 有人可以帮我吗,在此先谢谢!
I actually write for Baeldung too and am currently writing a small article about how to add security to websockets in Spring! 我实际上也为Baeldung撰写文章,目前正在写一篇有关如何在Spring中为Websocket添加安全性的小文章! There are just a few steps you need to do to get this all working! 您只需要做几个步骤,即可完成所有工作!
Backend-wise (since you said the UI was already done or being built, I'll just focus on the backend here), it really involves three parts: (1) the necessary POJO's, the controller, and the configuration. 在后端方面(由于您已经说过UI已经完成或正在构建,因此我将只关注后端),它实际上包括三个部分:(1)必要的POJO,控制器和配置。
Your POJO's will be very simple - here we just use Greeting and Message which specify a name and basic text data type (I'll skip over this here to save space but you can see it in the resource below). 您的POJO非常简单-在这里我们只使用Greeting和Message来指定名称和基本文本数据类型(我将在此处跳过以节省空间,但是您可以在下面的资源中看到它)。
Your controller will look like this: 您的控制器将如下所示:
@Controller
public class GreetingController {
@MessageMapping("/hello")
@SendTo("/topic/greetings")
public Greeting greeting(HelloMessage message) throws Exception {
Thread.sleep(1000); // simulated delay
return new Greeting("Hello, " + message.getName() + "!");
}
}
Take a look at the annotations - those are really what set this controller apart from say a normal REST controller. 看一看注释-这些实际上是使该控制器与普通REST控制器不同的地方。
And your configuration looks like this - again take a look at the annotations - particularly '@EnableWebSocketMessageBroker' - and the class 'AbstractWebSocketMessageBrokerConfigurer': 您的配置看起来像这样-再次查看注释-特别是'@EnableWebSocketMessageBroker'-和类'AbstractWebSocketMessageBrokerConfigurer':
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic");
config.setApplicationDestinationPrefixes("/app");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/gs-guide-websocket").withSockJS();
}
}
A look at this great resource too: https://spring.io/guides/gs/messaging-stomp-websocket/ 看看这个伟大的资源: https : //spring.io/guides/gs/messaging-stomp-websocket/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.