如题,网上找了相关资料查明是websocket连接超时的问题。使用了反向代理,因此超过60S没有数据传输的连接会断开。
把代理的那个超时时间设置长一点,无限长。你开什么玩笑!那还代理个啥玩意。
解决方法一:后台写个定时程序每<60s频率给前端的socket发个消息就好了。
import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import javax.annotation.Resource; /** * @Auther: lanhaifeng * @Date: 2019/5/16 0016 14:29 * @Description:webSocket定时发送消息类 * @statement: 以<60s的频率发送给websocket连接的对象,以防止反向代理的60s超时限制 */ @Configuration //1.主要用于标记配置类,兼备Component的效果。 @EnableScheduling // 2.开启定时任务 public class SaticScheduleTask { @Resource private WebSocketServer webSocketServer; //3.添加定时任务 //@Scheduled(cron = "0/5 * * * * ?") //或直接指定时间间隔,例如:5秒 @Scheduled(fixedRate=58*1000) private void configureTasks() throws Exception{ webSocketServer.sendMessage("连接成功"); } }
前提是你的websocket是多线程模式,否则多个socket连接会因为一个session的问题报错。参考spring boot集成Websocket
所有评论(0)