0

I am trying to send PongMessage to a server from web-socket client (heart-beat message you may say).

In server, I have written a method like this inside a class Annotated with @ServerEndpoint :

@OnMessage
        public void onPong(PongMessage pongMessage, Session session) {

          LOGGER.log(Level.INFO, " -- Got Hit Yaay!! -- ");

                try {
                    session.getBasicRemote().sendText(PONG_RECEIVED);
                } catch (IOException e) {
                    e.printStackTrace();
                }

        }

PongMessage Accepts ByteBuffer according to an oracle documentation. I tried generating a bytebuffer in server and sending exact same generated bytebuffer value from socket client. Server still throws an exception saying it Cannot decode a string.

I am not able to hit onPong() method. Is there any format for pongmessage that client can send to a websocket server so that onPong() method gets invoked? What am I doing wrong here? Any Ideas ?

ishwor kafley
  • 938
  • 14
  • 32
  • Which websocket client are you using? – Rafael Paulino Feb 20 '18 at 11:19
  • @RafaelPaulino a chrome extension called Simple Websocket Client. This is just for testing purpose. But later, Angular application will be a web-socket client. – ishwor kafley Feb 20 '18 at 11:27
  • I have found an old [post](https://stackoverflow.com/questions/10585355/sending-websocket-ping-pong-frame-from-browser) that Ping/Pong messages should be handled by the browser. If that is still true (i'm not sure), then you should be moving to a "custom" solution: don't use PongMessage. Receive some JSON defined by you. – Rafael Paulino Feb 20 '18 at 11:55
  • @RafaelPaulino I tried sending ping message to a browser from server after having a successful web-socket connection. The browser didn't receive the ping control frame that are being sent over by a server nor the server got the response back from browser. Sending JSON will work though. Copy paste your solution into the answer box , I will give you up an vote, It partially solves my problem :) – ishwor kafley Feb 20 '18 at 12:42

2 Answers2

1

I have found an old post that Ping/Pong messages should be handled by the browser. If that is still true (i'm not sure), then you should be moving to a "custom" solution: don't use PongMessage. Receive some JSON defined by you.

Rafael Paulino
  • 570
  • 2
  • 9
0

currently no major browser supports ping nor pong sending (neither periodical nor on demand using some JS function), so the Chrome extension you were using probably sends some custom text message that requires custom handling on the server (text message frames and ping/pong message frames have different opcodes, which tell the receiver (java server in your case) which OnMessage to call).

All browsers will however automatically correctly respond to a ping with a valid pong out-of-the-box. Therefore currently the best option is to send pings from your server side.
You can also send just pongs from the server, if you only need keep-alives, although bare in mind that if you send an unsolicited pong to a client that does sends pings (like a java client for example that has such capabilities) it may get confused that it was a malformed pong to his ping.

morgwai
  • 2,513
  • 4
  • 25
  • 31