In my websocket server I am retreiving a cutom header which I wan't link to the session it is from. I already got the a working ServerEndpointConfig.Configurator which looks like this:
public class WebSocketConfig extends ServerEndpointConfig.Configurator {
private static final Logger LOG = LoggerFactory.getLogger(WebSocketConfig.class);
@Override
public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
Map<String, List<String>> headers = request.getHeaders();
if (headers != null) {
if (headers.containsKey("key")) {
List<String> header = headers.get("key");
if (header.size() > 0) {
LOG.info(header.get(0));
}
}
}
}
}
My ServerEndpoint looks like this:
@ServerEndpoint(value = "/websocket", configurator = WebSocketConfig.class)
public class WebSocket {
private static final Logger LOG = LoggerFactory.getLogger(WebSocket.class);
public WebSocket() {
}
@OnOpen
public void onOpen(Session session) {
LOG.info("OnOpen");
}
@OnClose
public void onClose(Session session) {
LOG.info("OnClose");
}
@OnMessage
public void onMessage(String message, Session session){
LOG.info("OnMessage: " + message);
}
}
In the function onOpen I want to save link the session to the header value. But how can I access it?
Regards.