WebSocket连接异常 Error parsing HTTP request header Connection reset by peer

问题描述

在使用spring的方式集成websocket时,在配置WebSocketConfigurer后

@Configuration
@EnableWebSocket
public class WebSocketConfiguration implements WebSocketConfigurer {
    @Resource
    ServletWebSocketServerHandler servletWebSocketServerHandler;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        // 配置处理器
        registry.addHandler(servletWebSocketServerHandler, "/")
                // 配置拦截器
                .addInterceptors(new ServletWebSocketHandshakeInterceptor())
                // 解决跨域问题
                .setAllowedOrigins("*");
    }
}

ServerEndpoint代码

@Component
@ServerEndpoint(value = "/websocket/{key}")
public class MqWebSocketServer extends AbstractWebSocketServer {
    @Override
    public void onOpen(Session session, String key) {

    }

    @Override
    public void onMessage(String message, Session session, String key) {

    }

    @Override
    public void onClose(Session session, String key) {

    }

    @Override
    public void onError(Session session, Throwable throwable) {

    }
}

用postman请求发现结果时 404 ,错误信息如下

Could not connect to ws://127.0.0.1:9095/spring-boot-websocket/websocket/123
10:04:52
Error: Unexpected server response: 404
Handshake Details
Request URL: http://127.0.0.1:9095/spring-boot-websocket/websocket/123
Request Method: GET
Status Code: 404 

WebSocket连接异常 Error parsing HTTP request header Connection reset by peer_第1张图片

错误原因

请求地址应为ws://127.0.0.1:9095/spring-boot-websocket,因为在实现WebSocketConfigurer后配置的路径为全路径,不是匹配路径

WebSocket连接异常 Error parsing HTTP request header Connection reset by peer_第2张图片

你可能感兴趣的:(websocket,websocket,http,网络协议)