netty-socketio icon indicating copy to clipboard operation
netty-socketio copied to clipboard

reuse data from AuthorizationListener

Open wangsenyan opened this issue 4 years ago • 3 comments

hi, i want to set data in AuthorizationListener then use the data in EventListener

so i don't have to decode token twice or more

what should i do ? thanks

wangsenyan avatar Dec 06 '21 13:12 wangsenyan

you can use the threadlocal 🤓

mylyed avatar Dec 09 '21 13:12 mylyed

well,threadlocal is unsafe in threadpool

wangsenyan avatar Dec 15 '21 11:12 wangsenyan

A workaround is to use the HttpHeaders in the handshake data. It doesn't look elegant but works.

AuthorizationListener

handshakeData.getHttpHeaders().add('YOUR_TOKEN_KEY', 'TOKEN_VALUE')

EventListener

public void onData(SocketIOClient client, MessagePayload data, AckRequest ackSender) {
    // fetch token from http headers in the handshake data
    final String token = client.getHandshakeData().getHttpHeaders().get('YOUR_TOKEN_KEY');
    // ...
}

plainheart avatar Dec 22 '21 12:12 plainheart