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

解读源码:websocket failed: Error during WebSocket handshake: Unexpected response code:500_第1张图片

 

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

解决:给websocket session 添加用户

public class WebSocketHandShakeInteceptor implements HandshakeInterceptor {
    @Override
    public boolean beforeHandshake(ServerHttpRequest request,
                                   ServerHttpResponse response,
                                   WebSocketHandler webSocketHandler,
                                   Map map) throws Exception {
        System.out.println("握手操作===========");
        if (request instanceof ServletServerHttpRequest) {
            ServletServerHttpRequest servletServerHttpRequest =
                    (ServletServerHttpRequest)(request);
            HttpSession session = servletServerHttpRequest.getServletRequest().getSession(false);
            if (session != null) {
                User user = (User) session.getAttribute("user");
                if(user == null){
                    user = new User();
                }
                map.put("user",user);
            }
        }
        System.out.println("=====握手成功========");
        return true;
    }

    @Override
    public void afterHandshake(ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse, WebSocketHandler webSocketHandler, Exception e) {

    }
}

你可能感兴趣的:(websocket)