Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
950 views
in Technique[技术] by (71.8m points)

java - I get a status 200 when connecting to the websocket, but it is an error?

My error shows up in the console of my browser:

"WebSocket connection to 'ws://localhost:32768/DspClusterWebServices/myHandler' failed: Unexpected response code: 200"

I am using Spring Websockets 4.1.5 and Tomcat 8.0.18. My WebSocketConfigurer implementation class looks like:

@Configuration
@Controller
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer
{
   class MyHandler implements WebSocketHandler
   {
      @Override
      public void afterConnectionEstablished(WebSocketSession session) throws Exception
      {
         System.out.println("afterConntectionEstablished called");
      }

       ...implements rest of functions with a System.out.println and false for supportsPartialMessages()

      }
   }

   @Override registerWebSocketHandlers(WebSocketHandlerRegistry registry)
   {
      registry.addHandler(myHandler(), "myHandler").withSockJS();
   }

   @Bean
   public WebSocketHandler myHandler()
   {
      return new MyHandler();
   }
}

My testWebsocketClient.js tries to connect with this code, but has a error code of 200:

websocket = new WebSocket("ws://localhost:8080/myApp/myHandler");

I cannot figure out what to try next. I thought that this would cause the afterConnectionEstablished(WebSocketSession session) method to fire? Isn't code 200 good?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Please check http://procbits.com/connecting-to-a-sockjs-server-from-native-html5-websocket!

After you append /websocket (to your URL), it will give you the error

Failed to parse Origin header value [null]

;) , which then will in turn lead you to that link.

You'll have to add .setAllowedOrigins("*") to your addHandler() method, and then it could finally work!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...