WebSocket connection to 'ws://localhost:8080/xxx' failed: Error during WebSocket handshake

2023-05-04 20:28:38 浏览数 (2)

WebSocket connection to ‘ws://localhost:8080/xxx’ failed: Error during WebSocket handshake: Unexpected response code: 200 websocket项目启动之后,页面控制台报错,后面发现是后台加了拦截器导致的,分享解决前后的代码情况。 ScoketConfig类:

代码语言:javascript复制
    ...

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(handler(), "/webSocket").setAllowedOrigins("*")
            .addInterceptors(new MyHandshakeInterceptor());
        registry.addHandler(handler(), "/webSocket/socketJs").setAllowedOrigins("*")
        .addInterceptors(new MyHandshakeInterceptor()).withSockJS();
    }

    //静态内部类-拦截器
    private static class MyHandshakeInterceptor implements HandshakeInterceptor{
        ...
    }

去掉拦截器后,ScoketConfig类::

代码语言:javascript复制
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(handler(), "/webSocket");
        registry.addHandler(handler(), "/webSocket/socketJs").withSockJS();
    }

0 人点赞