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
  • 908
  • 14
  • 31
  • 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

1 Answers1

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
  • 562
  • 2
  • 9