Simple Session Management for Jetty Websockets

For tracking my clients websockets on the server, I need to store the session id for each HTTP connection. Otherwise I can't  distinguish the clients, as soon as they open more than one websocket.

My current websocket backend is a very simple Jetty-based Java application. When trying to get the HTTP session id in my websocket handler, I got a No SessionManager found error. Browsing the web provided several complex solutions [1] [2] that I could reduce this very simple session handling setup, using jetty's reasonable defaults.
Server server     = new Server(8001);

SessionHandler sh = new SessionHandler(); // org.eclipse.jetty.server.session.SessionHandler
sh.setHandler(websocketHandler);          // wrap websocket handler in session handler

server.setHandler(sh);                    // set session handler as jettys default handler
server.start();
Adding just two lines of code and changing the server's default handler I got basic session handling in my jetty app. Very nice.

Ciao,
Juve

Comments

Unknown said…
What class is websocketHandler??

How you can get connections?

thank you!!!
Stepherd said…
I am expecting more interesting topics from you. And this was nice content and definitely it will be useful for many people.

Staffing Companies in Bangalore