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().