Spring Boot Stomp Websocket在本地工作,但在已部署的服务器上不起作用
我有一个踩踏客户端和弹簧后端,该代码在本地时可以正常工作,但部署到服务器时则无法连接到服务器。
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic");
config.setApplicationDestinationPrefixes("/app");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/tracker").setAllowedOrigins("*");
}
}
JavaScript客户端启动与: var socket = new WebSocket(“ ws:// localhost:8080/tracker”);
当我在部署后尝试一下
websocket(“ wss:// myurl/tracker”)
或
websocket(“ wss:// myurl:8080/tracker”)
连接失败
I have a STOMP client and Spring backend, the code works fine when local but not when deployed to server, failed to connect to server.
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic");
config.setApplicationDestinationPrefixes("/app");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/tracker").setAllowedOrigins("*");
}
}
Javascript client initiates connection with :
var socket = new WebSocket("ws://localhost:8080/tracker");
When I try this after deployment
WebSocket("wss://myurl/tracker")
or
WebSocket("wss://myurl:8080/tracker")
The connection fails
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(1)
您能提供更多详细信息吗?确切的错误代码和消息是什么?
部署服务器前面是否有反向代理或负载平衡器?这可能会阻止Websocket在部署环境中连接。在这种情况下,您需要配置代理/平衡器以允许Websockets。在NGINX中,这些是您要查找的行:
WebSocket连接以HTTP升级请求开始,以根据安全性将协议升级到WS或WSS。上面的行指示NGINX将请求进一步传递给服务器。
我建议您在
Can you provide more details? What is the exact error code and message?
Is it possible that you have a reverse proxy or a load balancer in front of your deployment server? This could be blocking the websocket from connecting in the deployment environment. In that case, you need to configure the proxy/balancer to allow for websockets. In NGINX, these are the lines you are looking for:
Websocket connection starts with an HTTP Upgrade request to upgrade the protocol to WS or WSS depending on security. The lines above instruct NGINX to pass that request further to the server.
I recommend you have an in-depth read here in NGINS's guide.