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

前端 未结 1 1108
野的像风
野的像风 2020-12-29 08:59

I have this code

@ServerEndpoint(value = \"/websocket\")
public class Service {
    private String clientId; 
    @OnOpen
    public void init(Session sessi         


        
相关标签:
1条回答
  • 2020-12-29 09:32

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

    0 讨论(0)
提交回复
热议问题