问题
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