Spring Boot Stomp Websocket在本地工作,但在已部署的服务器上不起作用

发布于 2025-02-08 12:09:50 字数 677 浏览 3 评论 0原文

我有一个踩踏客户端和弹簧后端,该代码在本地时可以正常工作,但部署到服务器时则无法连接到服务器。

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笔落惊风雨 2025-02-15 12:09:50

您能提供更多详细信息吗?确切的错误代码和消息是什么?

部署服务器前面是否有反向代理或负载平衡器?这可能会阻止Websocket在部署环境中连接。在这种情况下,您需要配置代理/平衡器以允许Websockets。在NGINX中,这些是您要查找的行:

proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";

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:

proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文