The invention discloses a method and device for disconnecting and reconnecting websocket, which includes steps: A. monitoring the connection events of websocket; B. counting the number of failed websocket connections when websocket connections fail; C. reconnecting websocket when the number of failed websocket connections reaches the threshold; and realizing the reconnection of broken websocket through the scheme of the invention; In the reconnection of websocket, dependency injection is added, and control inversion is completed in the process of reconnection, so that vuex can obtain the previous program state information after disconnection reconnection, and continue to manage the program state.
【技术实现步骤摘要】
一种websocket断线重连方法及装置
本专利技术涉及教育系统领域,尤其涉及一种websocket断线重连方法及装置。
技术介绍
目前项目的技术栈为vue+websocket,在断线重连后,websocket有断线重连机制,但是vue的状态管理工具vuex没有断线重连机制,在断线重连后,程序的状态混乱,导致出错。因此,现有技术还有待于改进和发展。
技术实现思路
鉴于上述现有技术的不足,本专利技术的目的在于提供一种websocket断线重连方法及装置,旨在解决websocket断线重连的问题。本专利技术的技术方案如下:一种websocket断线重连方法,其中,包括步骤:A.对websocket的连接事件进行监听;B.当websocket连接失败时,对websocket连接失败次数进行计数;C.当websocket连接失败次数达到阈值时,重新连接websocket。所述的websocket断线重连方法,其中,所述步骤A之前包括:定义一个计数器,记录websocket连接失败次数,并设定计数器的阈值,当websocket连接失败次数达到阈值时,计数器触发重连函数。所述的w ...
【技术保护点】
1.一种websocket断线重连方法,其特征在于,包括步骤:A.对websocket的连接事件进行监听;B.当websocket连接失败时,对websocket连接失败次数进行计数;C.当websocket连接失败次数达到阈值时,重新连接websocket。
【技术特征摘要】
1.一种websocket断线重连方法,其特征在于,包括步骤:A.对websocket的连接事件进行监听;B.当websocket连接失败时,对websocket连接失败次数进行计数;C.当websocket连接失败次数达到阈值时,重新连接websocket。2.根据权利要求1所述的websocket断线重连方法,其特征在于,所述步骤A之前包括:定义一个计数器,记录websocket连接失败次数,并设定计数器的阈值,当websocket连接失败次数达到阈值时,计数器触发重连函数。3.根据权利要求2所述的websocket断线重连方法,其特征在于,所述重连函数包括websocket的连接的地址,当websocket连接失败次数达到阈值时,通过预先设置的连接地址进行重连。4.根据权利要求3所述的websocket断线重连方法,其特征在于,所述步骤B包括:当websocket第一次连接失败时,判断websocket连接失败类型;当websocket连接失败类型为连接出错时,关闭websocket连接,并且清除计数;对websocket连接出错的数据进行异步处理,并重新连接。5.根据权利要求4所述的websocket断线重连方法,其特征在于,所...
【专利技术属性】
技术研发人员:李天驰,孙悦,
申请(专利权)人:深圳点猫科技有限公司,
类型:发明
国别省市:广东,44
还没有人留言评论。发表了对其他浏览者有用的留言会获得科技券。