pippo icon indicating copy to clipboard operation
pippo copied to clipboard

There is no convenient way to access http session from web sockets.

Open whyicantusemyemailasusername opened this issue 6 years ago • 3 comments

The only way I found is to cast WebSocketContext.connection to container-specific classes.

I use the same approach. See in demo.

decebals avatar Nov 22 '18 19:11 decebals

You can find more technical details (implementation details) about WebSocket support in Pippo at https://github.com/pippo-java/pippo/pull/360.

The same approach with cast WebSocketConnection to specific container implementation (JettyWebSocketConnection, UndertowWebSocketConnection) I used when I use Undertow.

WebSocketConnection connection = webSocketContext.getConnection();
// cast WebSocketConnection to UndertowWebSocketConnection
UndertowWebSocketConnection undertowConnection = (UndertowWebSocketConnection) connection;
WebSocketHttpExchange exchange = undertowConnection.getExchange();
WebSocketChannel channel = undertowConnection.getChannel();

The idea is that we cannot use an generic API (a subset of Java Servlet API) for WebSocket to have an easy access to HttpSession or other elements from Servlet API. Each container come with a custom definition.

decebals avatar Nov 24 '18 11:11 decebals

I add below some useful links that can help us to polish more the websocket support in Pippo:

  • https://stackoverflow.com/questions/20240591/websocket-httpsession-returns-null
  • https://stackoverflow.com/questions/17936440/accessing-httpsession-from-httpservletrequest-in-a-web-socket-serverendpoint

decebals avatar Nov 24 '18 12:11 decebals