简体   繁体   English

自定义websocket握手

[英]Custom websocket handshake

I have a ServerEndpoint and a ClientEndpoint exchanging JSON messages via the respective encoders/decoders. 我有一个ServerEndpointClientEndpoint通过相应的编码器/解码器交换JSON消息。

Now I want to add the ability for a client to choose a unique user name. 现在,我想为客户端添加选择唯一用户名的功能。

Let's say I have a fun isNameAvailable(name: String): Boolean (on the server, of course). 假设我有一个fun isNameAvailable(name: String): Boolean (当然在服务器上)。

Haven't tried it, but I do think I could use a WebFilter s to guard access to WebSocket endpoints. 还没有尝试过,但是我确实可以使用WebFilter来保护对WebSocket端点的访问。 (eg let's say I connect to ws://localhost:8888/ws/foo and I add a web filter that intercepts all accesses to /ws/* ) in which I'd query the user repeatedly for a user name and reject it until isNameAvailable returns true . (例如,假设我连接到ws://localhost:8888/ws/foo并添加了一个Web过滤器,该过滤器拦截对/ws/*所有访问),在该过滤器中,我会反复向用户查询用户名并拒绝它直到isNameAvailable返回true为止。

But in my understanding, that fails as soon as I add a non-browser client. 但是据我了解,一旦添加非浏览器客户端,该操作就会失败。

How can you write a "custom handshake"? 您如何编写“自定义握手”?

Ie I want to repeatedly query the client for a String from within the ServerEndpoint 's @OnOpen until one fits and only then continue with the rest of the setup, after which the client and the server can start exchanging the JSON messages. 也就是说,我想从ServerEndpoint@OnOpen反复查询客户端的String ,直到适合为止,然后再继续其余的设置,此后客户端和服务器可以开始交换JSON消息。 That is, something like this: 也就是说,像这样:

@ServerEndpoint("/foo")
class FooServerEndpoint{
    @OnOpen
    fun open(session:Session){
        var userName:String
        do{
            userName = requestUserName(session)
        }while(!acceptUserName(userName,session)
    }
}

Is there a way to do that? 有没有办法做到这一点?

UPDATE: Technology Used 更新:使用的技术

JavaEE 8 / Kotlin 1.3.21 , deployed to a Glassfish 5 server and connected to by a Glassfish Tyrus 1.15 -based client JavaEE 8 / Kotlin 1.3.21 ,已部署到Glassfish 5服务器并由基于Glassfish Tyrus 1.15的客户端连接

Maybe have a look at creating a custom ServerEndpointConfig.Configurator and overload the modifyHandshake() method and apply this instance to your server endpoint as the configurator property of the @ServerEndpoint annotation? 也许看看创建一个自定义ServerEndpointConfig.Configurator并重载ModifyHandshake()方法,然后将此实例作为@ServerEndpoint批注的configurator属性应用于您的服务器端点? Similar answer here: https://stackoverflow.com/a/21766822/11133168 类似的答案在这里: https : //stackoverflow.com/a/21766822/11133168

You could also find more details on this topic in the book " The Java EE 7 Tutorial " in Chapter 18.10 您还可以在第18.10章的“ Java EE 7教程 ”一书中找到有关此主题的更多详细信息。

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

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