Receiving pings with Java EE Websocket API

2019-04-10 14:25发布

问题:

I'm currently working with the Tyrus reference implementation of the Java Websocket API. I've successfully created a server endpoint that receives binary messages, text messages, and pong messages, but I'm stuck trying to get it to receive ping messages. I've searched through much of the Tyrus source code and read the Jave EE Websocket tutorial but neither demonstrate functionality for receiving pings (only for sending them). Does anyone know if this is something not possible with the current API? If not, could you point me in the right direction for receiving ping messages?

回答1:

You cannot process ping messages. JSR 356 (Java API for WebSocket) spec does state that the implementation have to always respond to ping without giving application any opportunity to interact with those requests.

You can only send pings and consume pongs:

@OnMessage
public void onPong(PongMessage pongMessage) {
    //...
}

Why do you want to do that?