当前位置:网站首页>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 .
边栏推荐
- Fabric of kubernetes CNI plug-in
- Check 15 developer tools of Alibaba
- What are the advantages of automation?
- Application of safety monitoring in zhizhilu Denggan reservoir area
- 【Day1】 deep-learning-basics
- Hands on deep learning (42) -- bi-directional recurrent neural network (BI RNN)
- 使用 C# 提取 PDF 文件中的所有文字(支持 .NET Core)
- AUTOSAR从入门到精通100讲(106)-域控制器中的SOA
- Hands on deep learning (37) -- cyclic neural network
- Summary of the most comprehensive CTF web question ideas (updating)
猜你喜欢
Summary of reasons for web side automation test failure
Servlet基本原理与常见API方法的应用
How web pages interact with applets
【Day1】 deep-learning-basics
A little feeling
PHP personal album management system source code, realizes album classification and album grouping, as well as album image management. The database adopts Mysql to realize the login and registration f
PHP代码审计3—系统重装漏洞
Hands on deep learning (44) -- seq2seq principle and Implementation
技术管理进阶——如何设计并跟进不同层级同学的绩效
自动化的优点有哪些?
随机推荐
Launpad | Basics
Golang Modules
Hands on deep learning (39) -- gating cycle unit Gru
uniapp---初步使用websocket(长链接实现)
Exercise 9-1 time conversion (15 points)
System.currentTimeMillis() 和 System.nanoTime() 哪个更快?别用错了!
Exercise 7-2 finding the maximum value and its subscript (20 points)
Write a mobile date selector component by yourself
Custom type: structure, enumeration, union
入职中国平安三周年的一些总结
Golang defer
对于程序员来说,伤害力度最大的话。。。
Upgrading Xcode 12 caused Carthage to build cartfile containing only rxswift to fail
Write a jison parser (7/10) from scratch: the iterative development process of the parser generator 'parser generator'
转载:等比数列的求和公式,及其推导过程
Normal vector point cloud rotation
Dynamic address book
mmclassification 标注文件生成
7-17 crawling worms (15 points)
Golang type comparison