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 Answer
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?
5 Comments
11th Hour Worker
Well I'm trying to make a websocket proxy server. One of the clients I want to connect to my proxy server regularly sends pings to check the connection status. I noticed you are the Tyrus developer. How hard do you think it will be to hack the library to put a hook for my app. Thanks for your reply btw!
Pavel Bucek
interesting usecase.. and no, this should not be hard to do; I think difficult part would be to perform the handshake and properly pass the info to the original client when something goes wrong.. and if you'd like to pass upgrade response headers back to the client - that will be difficult. Otherwise it should be ok. We could even uptake your changes into Tyrus if you make your feature configurable..
Dennis Laumen
@pavel-bucek did you solve this issue? I stumbled upon this missing feature as well but I'm not sure it's been solved in the meantime. (Not using Tyrus but using the JBoss WebSocket API in Undertow)
11th Hour Worker
@DennisLaumen I've since moved on, but I can say it only took me 2 days to hack Tyrus and use my own internal Tyrus build to make WebSockets work. Obviously not ideal but all the other implementations of WebSockets at the time also didn't have this feature so I made do.
Dennis Laumen
@2300Worker thanks for the response after all these years ;-). I'm currently using an alternative solution for keep alive we already had mostly in place. Using pings is an optimization which isn't essential yet but might be in the future.