简体   繁体   English

如何在java spring websocket中实现乒乓球

[英]how to implement ping pong in java spring websocket

As per the documentation here https://tools.ietf.org/html/rfc6455#page-37 , a websocket must respond to a ping request with pong . 根据此处https://tools.ietf.org/html/rfc6455#page-37上的文档, websocket必须使用pong响应ping请求。

I have implemented spring boot websocket to handle the websocket connections from clients. 我已经实现了spring boot websocket来处理来自客户端的websocket连接。

I have a websocket client implemented which sends ping frames periodically. 我实现了一个websocket客户端,该客户端定期发送ping frames

I found it here https://stackoverflow.com/a/24438328 that I cannot process ping messages in java. 我在这里https://stackoverflow.com/a/24438328找到了它,我无法在Java中处理ping消息。

I have queries regarding this - 我对此有疑问-

  1. Should I have to handle these ping requests in java spring-boot websocket ? 我应该在java spring-boot websocket处理这些ping请求吗?
  2. if yes how could I implement this in spring boot application and respond to a ping request with pong ? 如果是,我该如何在spring boot应用程序中实现此功能并使用pong响应ping请求?

Should I have to handle these ping requests in java spring-boot websocket? 我是否必须在java spring-boot websocket中处理这些ping请求?

No 没有

The server will automatically reply with a pong. 服务器将自动以乒乓球答复。

For Tomcat see 对于Tomcat,请参见

https://github.com/apache/tomcat85/blob/b21d02042c408053a1944b27c3196ec9be3db381/java/org/apache/tomcat/websocket/WsFrameBase.java#L348 https://github.com/apache/tomcat85/blob/b21d02042c408053a1944b27c3196ec9be3db381/java/org/apache/tomcat/websocket/WsFrameBase.java#L348

https://github.com/apache/tomcat85/blob/b21d02042c408053a1944b27c3196ec9be3db381/java/org/apache/tomcat/websocket/WsFrameBase.java#L41-L42 https://github.com/apache/tomcat85/blob/b21d02042c408053a1944b27c3196ec9be3db381/java/org/apache/tomcat/websocket/WsFrameBase.java#L41-L42

For Jetty 对于码头

https://github.com/eclipse/jetty.project/blob/b0f34fec3f4156c6fb250132fe95a895e07fb5ae/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/events/AbstractEventDriver.java#L125-L143 https://github.com/eclipse/jetty.project/blob/b0f34fec3f4156c6fb250132fe95a895e07fb5ae/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/events/AbstractEventDriver.java#L125- L143

The ping is not exposed in the spring websocket handler (receive part) ping没有在弹簧websocket处理程序中暴露(接收部件)

With jetty it's possible to get notified for the ping (see org.eclipse.jetty.websocket.api.WebSocketPingPongListener ) 使用jetty可以收到有关ping的通知(请参阅org.eclipse.jetty.websocket.api.WebSocketPingPongListener

But it's not used by spring-websocket. 但是spring-websocket并未使用它。

Extend class TextWebSocketHandler and write your logic in handleTextMessage method. 扩展类TextWebSocketHandler并在handleTextMessage方法中编写逻辑。 If you are looking for more generic approach look into WebSocketHandler interface. 如果您正在寻找更通用的方法,请查看WebSocketHandler接口。 But as Antoniossss suggested ping pong is already implemented on protocol level for checking if connection is still alive. 但是正如Antoniossss建议的那样,乒乓球已经在协议级别实现,用于检查连接是否仍然有效。

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

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