CodedBy You
CodedBy You

Reputation: 11

Jetty/javalin getting HttpSession from websocket returns null always

Hello guys so am using javalin(uses jetty) and trying to get HttpSession but it returns null always! is there any better way i can get HttpSession in websockets without context?

    app.ws("/console",webSocketHandler -> {
           webSocketHandler.onConnect(wsSession -> {
               System.out.println(wsSession.getLocalAddress().getHostString() + " is Now Connected!");wsSession.send("ge");
           });
           webSocketHandler.onMessage((wsSession, s) -> {
               System.out.println((wsSession().getUpgradeRequest().getSession() == null)+"");
    });          

Upvotes: 1

Views: 275

Answers (1)

Joakim Erdfelt
Joakim Erdfelt

Reputation: 49462

HttpSession is an HTTP feature.

Once you are upgraded into WebSocket, you are no longer HTTP, hence no HttpSession.

If you want information from HttpSession, obtain the HttpSession during the upgrade process and then grab what you need from it. Because once you are upgraded into WebSocket, that HttpSession is no longer valid for that request.

In Java you can do that with the Jetty WebSocketCreator or the JSR356 Configurator.

Upvotes: 3

Related Questions