Spring: send message to websocket clients(Spring:向 websocket 客户端发送消息)
问题描述
我正在构建一个使用 Spring Boot、RabbitMQ 和 WebSocket 作为 POC 的网络聊天,但我被困在最后一点:WebSockets
我希望我的 ws 客户端连接到特定端点,例如 /room/{id}
并且当新消息到达时,我希望服务器将响应发送给客户端,但我搜索了类似的东西并没有找到.
I'm building a webchat with Spring Boot, RabbitMQ and WebSocket as POC, but I'm stucked a the last point: WebSockets
I want my ws clients to connect to a specific endpoint, like /room/{id}
and when a new message arrives, I want the server to send the response to clients, but I searched for something similar and didn't found.
目前,当消息到达时,我使用RabbitMQ对其进行处理,例如
Currently, when the message arrives, I process it with RabbitMQ, like
container.setMessageListener(new MessageListenerAdapter(){
@Override
public void onMessage(org.springframework.amqp.core.Message message, Channel channel) throws Exception {
log.info(message);
log.info("Got: "+ new String(message.getBody()));
}
});
我想要的是,而不是记录它,我想将它发送给客户端,例如:websocketManager.sendMessage(new String(message.getBody()))
what I would like is, instead log it , I want to send it to the client, for example: websocketManager.sendMessage(new String(message.getBody()))
推荐答案
好的,我想我明白了,给需要的人,这里是答案:
Ok, I think I got it, for everyone who needs it, here is the answer:
首先,需要在 pom.xml 中添加 WS 依赖
first, you need to add WS dependencies to the pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-messaging</artifactId>
</dependency>
创建一个 WS 端点
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
// the endpoint for websocket connections
registry.addEndpoint("/stomp").withSockJS();
}
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/");
// use the /app prefix for others
config.setApplicationDestinationPrefixes("/app");
}
}
注意:我使用的是 STOMP,所以客户端应该像这样连接
Note: I'm using STOMP, so the clients should connect like this
<script type="text/javascript">
$(document).ready(function() {
var messageList = $("#messages");
// defined a connection to a new socket endpoint
var socket = new SockJS('/stomp');
var stompClient = Stomp.over(socket);
stompClient.connect({ }, function(frame) {
// subscribe to the /topic/message endpoint
stompClient.subscribe("/room.2", function(data) {
var message = data.body;
messageList.append("<li>" + message + "</li>");
});
});
});
</script>
然后,您可以简单地使用
Then, you can simply wire the ws messenger on your components with
@Autowired
private SimpMessagingTemplate webSocket;
并使用
webSocket.convertAndSend(channel, new String(message.getBody()));
这篇关于Spring:向 websocket 客户端发送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Spring:向 websocket 客户端发送消息
基础教程推荐
- 如何对 HashSet 进行排序? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 在螺旋中写一个字符串 2022-01-01