Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I pass a parameter to the @OnOpen method with JEE7 Websockets,

I have this code

@ServerEndpoint(value = "/websocket")
public class Service {
    private String clientId; 
    @OnOpen
    public void init(Session session) throws IOException {
         //opening a websocket
         // get clientId
         clientId = // Code here to get initialization parameter.
    }

}

How do I get initialization parameters from the client opening the socket?.

like image 792
Leo Avatar asked Feb 04 '14 17:02

Leo


1 Answers

Depends what do you mean by initialisation parameter. You can do something like this:

@ServerEndpoint(value = "/websocket/{clientId}")
public class Service {
    private volatile String clientId; 
    @OnOpen
    public void init(@PathParam("clientId") String clientId, Session session) throws IOException {
         this.clientId = clientId;
    }
}

Then you have do use following URL to access your endpoint: ws://host/contextPath/websocket/[clientId].

if you use query parameters, please see Session#getQueryString().

like image 184
Pavel Bucek Avatar answered Oct 13 '22 23:10

Pavel Bucek