简体   繁体   English

如何使用JEE7 Websockets将参数传递给@OnOpen方法,

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

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] . 然后,您使用以下URL访问您的端点: ws://host/contextPath/websocket/[clientId]

if you use query parameters, please see Session#getQueryString() . 如果使用查询参数,请参阅Session#getQueryString()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM