foxtrotuniform6969
foxtrotuniform6969

Reputation: 4125

Vert.x SockJS Socket `.webUser()` is always null

I'm following Vert.x 4's SockJS documentation and noticed that each SockJSSocket inside my handler has a .webSession() and a .webUser(). However, both these fields are empty aside from the .webSession().id()

I have an AuthHandler registered on the sub-router that this socket handler is on, but the SockJS Client in my frontend is not capable of sending credentials in the HTTP upgrade request.

How am I supposed to populate these fields for use?

Upvotes: 1

Views: 226

Answers (1)

injecteer
injecteer

Reputation: 20699

I'm using the following straight-forward code to keep the session-like information available:

class SockJSBridge implements Handler<BridgeEvent> {

  @Override
  void handle( BridgeEvent event ) {
    
    SockJSSocket socket = event.socket()
    MultiMap headers = socket.headers()

    switch( event.type() ){
      case REGISTER:
         def user = getUserFromJWTHeader event.rawMessage.headers.authorization
         headers.add 'userId', user.id
         break

      case SEND:
         String userId = headers.get 'userId'
         socket.write new JsonObject( address:userId, body:event.rawMessage?.body ).toBuffer()
         break
     }
  }
}

Upvotes: 1

Related Questions