我有这个代码
@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.
}
}
如何从客户端打开套接字来获取初始化参数?
解决方法
取决于你的初始化参数是什么意思你可以这样做:
@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;
}
}
然后,您将使用以下URL访问您的端点:ws:// host / contextPath / websocket / [clientId].
如果使用查询参数,请参阅Session#getQueryString().
