Pages

03 March, 2012

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

4 comments:

Emerson Moretto 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

henris said...

Thank you so much for posting like this. I enjoyed much more here. So please keep update like this.

Medical Insurance Plans

Health Insurance UAE

Insurance Companies in Dubai

Jissa said...

Thank you for sharing. And if you are looking for the best relocation process can make easier by our beneficial relocation services with

Warehouse for Rent Singapore

Cheapest Movers in Singapore

Cheapest Storage Space Singapore