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?.
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()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With