我正在尝试在从客户端接收连接时修改HandShakeResponse
。但问题是HandShakeResponse.getHeaders()
返回空地图
以下是手动配置器
public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response){
HttpSession session = (HttpSession) request.getHttpSession();
System.out.println("Print Here");
Map<String, List<String>> map = response.getHeaders();
ObjectMapper mapper = new ObjectMapper();
try {
System.out.println(mapper.writeValueAsString(map));
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(map.get(HandshakeResponse.SEC_WEBSOCKET_ACCEPT));
System.out.println("Print there?");
if (session == null){
response.getHeaders().put(HandshakeResponse.SEC_WEBSOCKET_ACCEPT, new ArrayList<String>());
}
else {
Integer userId = (Integer) session.getAttribute("userId");
if (userId == null){
response.getHeaders().put(HandshakeResponse.SEC_WEBSOCKET_ACCEPT, new ArrayList<String>());
}
else {
sec.getUserProperties().put("userId", userId);
}
}
}
我在输出终端
中关注了Print Here
{}
null
Print there?
我按照http://docs.oracle.com/javaee/7/tutorial/doc/websocket010.htm#BABJAIGH
执行所有操作