如何使用JEE7 Websockets将参数传递给@OnOpen方法, [英] how do I pass a parameter to the @OnOpen method with JEE7 Websockets,

查看:3841
本文介绍了如何使用JEE7 Websockets将参数传递给@OnOpen方法,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码

@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;
    }
}

然后您使用以下URL访问您的终端: ws:// host / contextPath / websocket / [clientId]

Then you have do use following URL to access your endpoint: ws://host/contextPath/websocket/[clientId].

如果您使用查询参数,请见 Session#getQueryString()

if you use query parameters, please see Session#getQueryString().

这篇关于如何使用JEE7 Websockets将参数传递给@OnOpen方法,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆