pippo
pippo copied to clipboard
There is no convenient way to access http session from web sockets.
The only way I found is to cast WebSocketContext.connection
to container-specific classes.
I use the same approach. See in demo.
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.
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