websocket failed: Error during WebSocket handshake: Unexpected response code: 200

Posted 蔡昊

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了websocket failed: Error during WebSocket handshake: Unexpected response code: 200相关的知识,希望对你有一定的参考价值。

websocket+spring整合,不能访问,

解读源码:

原因:websocket_username=null,导致空异常

解决:给websocket session 添加用户名

//握手前,读取用户名,并赋值给websocket的session
    @Override
    public boolean beforeHandshake(ServerHttpRequest request,
            ServerHttpResponse response, WebSocketHandler handler,
            Map<String, Object> attr) throws Exception {        
         if (request instanceof ServletServerHttpRequest) {
                ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) request;
                HttpSession session = servletRequest.getServletRequest().getSession(false);
                if (session != null) {
                    String userName = (String) session.getAttribute(Constants.SESSION_USERNAME);
                    if(userName == null){
                        userName = "WEBSOCKET_USERNAME_IS_NULL";
                    }
                    attr.put(Constants.WEBSOCKET_USERNAME,userName);
                }
            }
        return true;
    }

 

以上是关于websocket failed: Error during WebSocket handshake: Unexpected response code: 200的主要内容,如果未能解决你的问题,请参考以下文章

报错nginx failed error: during websocket handshake

failed: WebSocket 在连接建立之前关闭

如何修复 websocket-client Python 模块中的 CERTIFICATE_VERIFY_FAILED 错误?

SSL:尝试连接到 python3.7 中的 websocket 时出现 CERTIFICATE_VERIFY_FAILED

SpringBoot+WebSocket问题:Failed to register @ServerEndpoint class

Python Websocket 安全 [SSL: CERTIFICATE_VERIFY_FAILED] (_ssl.c:777)