关于在uniapp中使用websocket
前要
今天公司用uniapp开发的手机端应用需要用到websocket实时获取数据,在这里记录一下使用方法,以及对于断开重连,心跳机制用法的封装。
使用方法
先上代码
1 | uni.connectSocket({ |
使用uni.connectSocket()来进行连接,使用onSocketOpen和onSocketError来监听连接是否成功。
关于connectSocket()方法中的参数,具体请看官方介绍
- 其中complete参数,是接口调用结束之后的回掉,不管连接成功还是失败都会调用。
心跳机制,以及断开重连的封装使用
心跳机制是一种用于保持WebSocket连接的稳定性和活跃性的方法。
1 | <template> |
其他
- 一个在线测试websocket的网站 websocket-test
- 标题: 关于在uniapp中使用websocket
- 作者: DansRoh
- 创建于 : 2023-12-28 00:00:00
- 更新于 : 2024-06-24 17:16:49
- 链接: https://blog.shinri.me/2023/12/28/12_uniapp中使用webscoket/
- 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
评论