背景
当前项目中需要对websocket长时间的推送数据,但是在谷歌浏览器中就链接相对稳定,在安卓发布屏中会存在15min-60min或者更长时间的断开,报错信息如下:文章来源地址https://www.toymoban.com/news/detail-638942.html
报错信息
org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper.fillReadBuffer(NioEndpoint.java:1301),
org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper.read(NioEndpoint.java:1234),
org.apache.tomcat.websocket.server.WsFrameServer.onDataAvailable(WsFrameServer.java:75),
org.apache.tomcat.websocket.server.WsFrameServer.doOnDataAvailable(WsFrameServer.java:183),
org.apache.tomcat.websocket.server.WsFrameServer.notifyDataAvailable(WsFrameServer.java:162),
org.apache.tomcat.websocket.server.WsHttpUpgradeHandler.upgradeDispatch(WsHttpUpgradeHandler.java:156),
org.apache.coyote.http11.upgrade.UpgradeProcessorInternal.dispatch(UpgradeProcessorInternal.java:60),
org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:59),
org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:831),
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1629),
org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49),
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149),
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624),
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61),
java.lang.Thread.run(Thread.java:748)
网上提供两种方式:
- 修改反向代理,这种方式无法无限延长连接时常,组里尝试使用反向代理之后的链接直连,如果网络不通的话,此方式失效;
location / {
root html;
index index.html index.htm;
proxy_pass http://wslb;
proxy_http_version 1.1;
proxy_read_timeout 3600s;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
- 后端增加定时发送消息保活,修改之后发现可以正常不掉线了。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
/**
* @Description:webSocket定时发送消息类
* @statement: 以小于60s的频率发送给websocket连接的对象,以防止反向代理的60s超时限制
*/
@Configuration //1.主要用于标记配置类,兼备Component的效果。
@EnableScheduling // 2.开启定时任务
public class WebSocketSchedule {
@Autowired
private WebSocketServer webSocketServer;
//3.添加定时任务
//@Scheduled(cron = "0/30 * * * * ?")
//或直接指定时间间隔,例如:30秒
@Scheduled(fixedRate=30*1000)
public void configureTasks(){
webSocketServer.sendAliveMessage("pong");
}
}
文章来源:https://www.toymoban.com/news/detail-638942.html
到了这里,关于websocket报org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper.fillReadBuffer错误的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!