当前位置:网站首页>Uniapp--- initial use of websocket (long link implementation)
Uniapp--- initial use of websocket (long link implementation)
2022-07-04 10:03:00 【Penguin wants to go to galaxy to think about life!!!】
onLoad() {
this.connectSocketInit()
},
// // When you enter this page, create websocket Connect 【 The whole page is ready to use 】
connectSocketInit() {
// Judge whether there is websocket Connect , If any, don't go create sock Method
let socketTaskIsOpen = uni.getStorageSync("socketTaskIsOpen")
if (socketTaskIsOpen != "") return
// // Create a this.socketTask object 【 send out 、 receive 、 close socket Are manipulated by this object 】
this.socketTask = uni.connectSocket({
// 【 It's very important 】 Make sure your server is successful , If it's a mobile phone test, don't use it ws://127.0.0.1:9099【 A particularly easy mistake to make 】
url: config.monitorSocketBaseUrl,
success(data) {
console.log(" Whether online websocket Successful connection ");
uni.setStorageSync('socketTaskIsOpen', true)
},
})
// This method is assigned a value here to split these methods , Prevent too much code in an initialization method .
this.socketTaskIsOpen = true;
this.socketTask.onOpen = this.connectSocketOnOpen();
this.socketTask.onClose = this.connectSocketonClose()
this.socketTask.onMessage = this.connectSocketonMessage();
this.socketTask.onError = this.connectSocketonError()
},
// Send message when opening
connectSocketOnOpen(e) {
// The sending and receiving of messages must be in normal connection , To send or receive 【 Otherwise it will fail 】
// notes : Only the connection is normally open , To send messages normally and successfully
var jsonObject = uni.getStorageSync("MonitorInfo")
// If you want to json Format through websocket Send the past through
// json.stringfy() To transform
var jsonString = JSON.stringify(jsonObject)
// there timer The component implements a long link , Send packets at intervals .
this.timer = setInterval(() => {
try {
this.getMonitorInfo()
jsonObject = uni.getStorageSync("MonitorInfo")
jsonString = JSON.stringify(jsonObject)
this.socketTask.send({
data: jsonString,
async success() {
console.log(" Whether the online message was sent successfully ");
},
});
} catch (err) {
console.log(' Whether online websocket Disconnect the :' + err);
}
}, 15000)
},
connectSocketonClose(e) {
// This is just event monitoring 【 If socket If it is closed, it will execute 】
this.socketTask.onClose(() => {
uni.setStorageSync('socketTaskIsOpen', false)
console.log(" Whether online websocket It's closed ")
})
},
// Listen for server events
connectSocketonMessage(e) {},
// Connection establishment failed to reconnect
connectSocketonError(e) {
// this.connectSocketInit();
}
matters needing attention :
1. If you want to json Format through websocket Send the past through json.stringfy() To convert before sending
2. Long links are achieved by websocketOnOpen Method to send by interval time .
3.connectSocketInit() Method to determine whether there is sock link , If any, it is not being created socket
4.connectSocketInit() Method for onOpen,onClose,onMessage Checked and scored , Prevent various methods from coupling .
边栏推荐
- mmclassification 标注文件生成
- Custom type: structure, enumeration, union
- About the for range traversal operation in channel in golang
- Qtreeview+ custom model implementation example
- 今日睡眠质量记录78分
- Exercise 7-2 finding the maximum value and its subscript (20 points)
- C # use smtpclient The sendasync method fails to send mail, and always returns canceled
- 六月份阶段性大总结之Doris/Clickhouse/Hudi一网打尽
- 对于程序员来说,伤害力度最大的话。。。
- 【Day2】 convolutional-neural-networks
猜你喜欢
基于线性函数近似的安全强化学习 Safe RL with Linear Function Approximation 翻译 1
El Table Radio select and hide the select all box
【Day2】 convolutional-neural-networks
品牌连锁店5G/4G无线组网方案
SSM online examination system source code, database using mysql, online examination system, fully functional, randomly generated question bank, supporting a variety of question types, students, teache
Hands on deep learning (38) -- realize RNN from scratch
Web端自动化测试失败原因汇总
PHP book borrowing management system, with complete functions, supports user foreground management and background management, and supports the latest version of PHP 7 x. Database mysql
Devop basic command
法向量点云旋转
随机推荐
Ultimate bug finding method - two points
Devop basic command
智能网关助力提高工业数据采集和利用
2. Data type
Golang Modules
Hands on deep learning (32) -- fully connected convolutional neural network FCN
Exercise 9-4 finding books (20 points)
Hands on deep learning (45) -- bundle search
Golang defer
AUTOSAR from getting started to mastering 100 lectures (106) - SOA in domain controllers
Luogu deep foundation part 1 Introduction to language Chapter 4 loop structure programming (2022.02.14)
Launpad | Basics
7-17 crawling worms (15 points)
How web pages interact with applets
Write a mobile date selector component by yourself
Regular expression (I)
QTreeView+自定义Model实现示例
Exercise 7-8 converting strings to decimal integers (15 points)
View CSDN personal resource download details
Nuxt reports an error: render function or template not defined in component: anonymous