当前位置:网站首页>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 .
边栏推荐
- How to teach yourself to learn programming
- Application of safety monitoring in zhizhilu Denggan reservoir area
- How do microservices aggregate API documents? This wave of show~
- Mmclassification annotation file generation
- 六月份阶段性大总结之Doris/Clickhouse/Hudi一网打尽
- Dynamic address book
- libmysqlclient.so.20: cannot open shared object file: No such file or directory
- Go context basic introduction
- Modules golang
- Baidu R & D suffered Waterloo on three sides: I was stunned by the interviewer's set of combination punches on the spot
猜你喜欢

How can Huawei online match improve the success rate of player matching

Hands on deep learning (39) -- gating cycle unit Gru

Hands on deep learning (46) -- attention mechanism

C语言指针面试题——第二弹

【OpenCV 例程200篇】218. 多行倾斜文字水印

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

Matlab tips (25) competitive neural network and SOM neural network

How can people not love the amazing design of XXL job

libmysqlclient.so.20: cannot open shared object file: No such file or directory

【Day2】 convolutional-neural-networks
随机推荐
Write a jison parser from scratch (1/10):jison, not JSON
Qtreeview+ custom model implementation example
Deep learning 500 questions
入职中国平安三周年的一些总结
Hands on deep learning (43) -- machine translation and its data construction
MySQL transaction mvcc principle
Some summaries of the third anniversary of joining Ping An in China
PHP is used to add, modify and delete movie information, which is divided into foreground management and background management. Foreground users can browse information and post messages, and backgroun
Ultimate bug finding method - two points
MATLAB小技巧(25)竞争神经网络与SOM神经网络
uniapp---初步使用websocket(长链接实现)
Luogu deep foundation part 1 Introduction to language Chapter 4 loop structure programming (2022.02.14)
智能网关助力提高工业数据采集和利用
Get the source code in the mask with the help of shims
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
IIS configure FTP website
Golang Modules
ASP. Net to access directory files outside the project website
Pueue data migration from '0.4.0' to '0.5.0' versions
How to teach yourself to learn programming