Spring WebSocket 实例之间连接和发送消息 [英] Connecting and sending message between Spring WebSocket instances
问题描述
我有多个使用 Spring Boot WebSocket 的实例(根据 Spring 的前半部分创建)指南).我需要它们连接到特定主机名和端口的其他实例,并能够使用 STOMP 协议通过 websocket 连接发送消息.
I have multiple instances using Spring Boot WebSocket (created following the first half of Spring's guide). I need them to connect to other instances at specific hostnames and ports and to be able to send messages over the websocket connection using STOMP protocol.
不能回答我的问题的事情:
Things that don't answer my question:
- 我已阅读 Spring:向 websocket 客户端发送消息 和
在 Spring Websocket 上向特定用户发送消息 但这些和其他问题似乎都假设客户端已经启动了连接并且已经建立了用户和主题.这不是我的用例,因为我的服务既是服务器又是客户端. - 我没有使用集群,也没有像 Spring 那样跨实例共享会话tomcat集群中的Websocket
- I have read Spring: send message to websocket clients and Sending message to specific user on Spring Websocket but these and other questions seem to all assume that a client has already initiated a connection and that there are users and topics established. This is not my use case as my services are both server AND client.
- I am not using a cluster and I am not sharing sessions across instances as in Spring Websocket in a tomcat cluster
推荐答案
我找到了一些可以说明如何实现这一点的资源:
I have found some resources that cast some light on how to accomplish this:
https://www.sitepoint.com/implementing-spring-websocket-server-and-client/#javaspringchatclient
http://useof.org/java-open-source/org.springframework.messaging.simp.stomp.StompSessionHandler
第 3 项至少是一个完整的实现,但遗憾的是没有注释来解释正在发生的事情.
number 3 is at least a complete implementation but is unfortunately devoid of comments to explain what's going on.
这篇关于Spring WebSocket 实例之间连接和发送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!