java -ee websockets在Configurator中修改HandShakeResponse

时间:2014-10-25 23:47:56

标签: java-ee websocket tomcat7 handshake maven-tomcat-plugin

我正在尝试在从客户端接收连接时修改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

执行所有操作

0 个答案:

没有答案