Accessing native headers on StompHeaderAccessor

故事扮演 提交于 2020-01-06 03:19:15

问题


I'm building a chat server. I am using the following code to extract the username of a new user connection. It works, but it doesn't feel like the proper way to do this. How do I do this properly?

  @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);
  }
}

来源:https://stackoverflow.com/questions/51697277/accessing-native-headers-on-stompheaderaccessor

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!