我有一個在 Spring maven 框架中開發的 WebSocket 應用程式,當它部署在 localhost:8080/test 中時效果非常好,其中 test 是我的套接字端點。現在我將應用程式部署在我的 AWS 云服務器中,并嘗試使用相同的方法連接到云服務器,但使用特定的 URL(例如https://example.com/test),其中test是 WebSocket 端點和 URL( example.com) 是我的鏈接,類似于 localhost:8080。我還打開了安全組中必要連接所需的埠。
對于 localhost 它作業正常,但是當我嘗試將它連接到我的云服務器時,WebSocket 總是回傳狀態為 400 的錯誤函式。我不確定為什么會出現狀態 400,因為根據我的知識,狀態 400 意味著它是錯誤的請求。我做錯了什么伙計們?
我在下面粘貼我的java代碼
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic");
config.setApplicationDestinationPrefixes("/ws/");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/test").setAllowedOrigins("*"); // This will allow you to use ws://localhost:8080/test to establish websocket connection
registry.addEndpoint("/test").setAllowedOrigins("*").withSockJS(); // This will allow you to use http://localhost:8080/test to establish websocket connection
}
}
是我遺漏的任何配置還是我遺漏的任何代碼片段?
PS:我所有的測驗主要是使用 POSTMAN Websocket 請求完成的,是的!我使用 wss:// 例如.com
我的 POSTMAN URL 往往是wss://example.com/test ------> 不起作用
ws://localhost:8080/test -------> 這行得通
uj5u.com熱心網友回復:
我發現了這個問題。我一直在 Apache 服務器后面托管我的 tomcat 服務器,該服務器實際上將流量路由到 tomcat。所以我收到錯誤 Spring WebSocket: Handshake failed due to invalid Upgrade header: null。
我從Spring WebSocket 中找到了解決此問題的方法:Handshake failed due to invalid Upgrade header: null
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/439490.html
