Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get a cookie value inside websocket end-point

I'm using Websocket-API based on JavaEE 7, in my application.

I'm required to access the values set in cookies inside my websocket endpoint [Annotated one : @ServerEndpoint ("/websocket") ]. How would I do that?

@onOpen() method is there, which will be called automatically when a connection to this websocket endpoint is established. I want to access cookies values in there, inside this method.

I know how to do that in a servlet or JSP, but I'm new to Websockets.

Please help me doing this. Thanks in advance.

like image 253
Aryan Venkat Avatar asked Aug 07 '13 07:08

Aryan Venkat


People also ask

Can WebSockets use cookies?

Although, in theory, one could use cookies, as all WebSocket connections start with an HTTP request (with an upgrade header on it), and the cookies for the domain you are connecting to, will be sent with that initial HTTP request to open the WebSocket.

How do I get data from a WebSocket?

The Message event takes place usually when the server sends some data. Messages sent by the server to the client can include plain text messages, binary data, or images. Whenever data is sent, the onmessage function is fired.

How do I connect to a WebSocket endpoint?

In order to communicate using the WebSocket protocol, you need to create a WebSocket object; this will automatically attempt to open the connection to the server. The URL to which to connect; this should be the URL to which the WebSocket server will respond.

Is a WebSocket an endpoint?

The Web Socket Endpoint represents an object that can handle websocket conversations. Developers may extend this class in order to implement a programmatic websocket endpoint. The Endpoint class holds lifecycle methods that may be overridden to intercept websocket open, error and close events.


2 Answers

While Joakim's answer does provide a hint in the right direction I believe it does not fully answer the question, or at least can be complemented.

In order to retrieve the value of a cookie you must get the headers of the HandshakeRequest object, and look for the header named "cookie". Your modifyHandshake implementation will look something like:

public class MyEndpointConfigurator extends ServerEndpointConfig.Configurator {
    @Override
    public void modifyHandshake(ServerEndpointConfig config, 
                                HandshakeRequest request, 
                                HandshakeResponse response)
    {
        Map<String,List<String>> headers = request.getHeaders();
        config.getUserProperties().put("cookie",headers.get("cookie"));
    }
}
like image 57
gabouy Avatar answered Nov 15 '22 10:11

gabouy


Access to request parameters is done via the @ServerEndpoint(configurator=MyConfigurator.class) technique.

See other answer on how to access the HttpSession, as its techniques are very similar.

like image 29
Joakim Erdfelt Avatar answered Nov 15 '22 11:11

Joakim Erdfelt