简体   繁体   中英

Java websocket: How to receive PongMessage from websocket client and respond to this message

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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