Java – how to use jee7 WebSockets to pass parameters to the @ onopen method,
•
Java
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 open a socket from a client to get initialization parameters?
Solution
Depending on what your initialization parameters mean, you can do 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; } }
You will then use the following URL to access your endpoint: WS: / / host / contextpath / websocket / [ClientID]
If you use query parameters, see session #getquerystring()
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码