简体   繁体   English

Java Websocket:如何从Websocket客户端接收PongMessage并响应此消息

[英]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). 我正在尝试将PongMessage从Web套接字客户端发送到服务器(您可能会说心跳消息)。

In server, I have written a method like this inside a class Annotated with @ServerEndpoint : 在服务器中,我在用@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. PongMessage根据oracle文档接受ByteBuffer 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 . 我无法点击onPong() method Is there any format for pongmessage that client can send to a websocket server so that onPong() method gets invoked? 客户端可以发送给Websocket服务器的pongmessage有什么格式,以便调用onPong()方法吗? What am I doing wrong here? 我在这里做错了什么? Any Ideas ? 有任何想法吗 ?

I have found an old post that Ping/Pong messages should be handled by the browser. 我发现有一个旧帖子 ,Ping / Pong消息应由浏览器处理。 If that is still true (i'm not sure), then you should be moving to a "custom" solution: don't use PongMessage. 如果这仍然是正确的(我不确定),那么您应该转向“自定义”解决方案:不要使用PongMessage。 Receive some JSON defined by you. 接收您定义的一些JSON。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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