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

WebSocket connection to ‘ws://localhost:8080/xxx’ failed: Error during WebSocket handshake: Unexpected response code: 200
websocket项目启动之后,页面控制台报错,后面发现是后台加了拦截器导致的,分享解决前后的代码情况。
(本文章分享在CSDN平台,更多精彩请阅读 东陆之滇的csdn博客:http://blog.csdn.net/zixiao217)
ScoketConfig类:

    ...

    @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类::

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(handler(), "/webSocket");
        registry.addHandler(handler(), "/webSocket/socketJs").withSockJS();
    }
相关推荐
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页