访问StompHeaderAccessor上的本机头

时间:2018-08-05 18:42:54

标签: java spring stomp

我正在构建聊天服务器。我正在使用以下代码来提取新用户连接的用户名。它可以工作,但是感觉不像是正确的方法。如何正确执行此操作?

  @EventListener
  public void handleWebSocketConnectListener(SessionConnectedEvent event) {
    StompHeaderAccessor stompAccessor = StompHeaderAccessor.wrap(event.getMessage());
    GenericMessage connectHeader = (GenericMessage)stompAccessor.getHeader(SimpMessageHeaderAccessor.CONNECT_MESSAGE_HEADER);    // FIXME find a way to pass the username to the server
    Map<String, List<String>> nativeHeaders = (Map<String, List<String>>) connectHeader.getHeaders().get(SimpMessageHeaderAccessor.NATIVE_HEADERS);

    String login = nativeHeaders.get("login").get(0);
    String sessionId = stompAccessor.getSessionId();

    logger.info("Chat connection by user <{}> with sessionId <{}>", login, sessionId);
  }
}

0 个答案:

没有答案