-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPageTest.js
99 lines (96 loc) · 2.73 KB
/
PageTest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* @Author: WangYu
* @Date: 2019-07-23 10:32:45
* @Last Modified by: WangYu
* @Last Modified time: 2019-07-23 15:21:23
*/
export default {
data() {
return {
websock:null,
text:'',
flag:null
}
},
computed: {
},
methods: {
threadPoxi(){ // 实际调用的方法
//参数
const agentData = "Hello WebSockets!";
console.log('判断',this.websock.readyState,this.websock.OPEN,this.websock.CONNECTING)
//若是ws开启状态
if (this.websock.readyState === this.websock.OPEN) {
this.text += `连接正常,发送数据`
this.websocketsend(agentData)
}
// 若是 正在开启状态,则等待300毫秒
else if (this.websock.readyState === this.websock.CONNECTING) {
let that = this;//保存当前对象this
setTimeout(function () {
that.websocketsend(agentData)
}, 300);
this.text += `正在连接中,请勿频繁操作`
}
// 若未开启 ,则等待500毫秒
else {
this.text += `开启ws失败,五秒后尝试重连`
this.initWebSocket();
let that = this;//保存当前对象this
setTimeout(function () {
that.websocketsend(agentData)
}, 5000);
}
},
initWebSocket(){ //初始化weosocket
const wsuri = `ws://127.0.0.1:3000`//这个地址由后端童鞋提供
this.websock = new WebSocket(wsuri);
this.websock.onmessage = this.websocketonmessage;
this.websock.onopen = this.websocketonopen;
this.websock.onerror = this.websocketonerror;
this.websock.onclose = this.websocketclose;
},
websocketonopen(){ //连接建立之后执行send方法发送数据
let data = "Hello WebSockets!"
this.websocketsend(data)
},
websocketonerror(){//连接建立失败重连
console.log('连接建立失败重连')
this.text += '与ws://127.0.0.1:3000连接建立失败,正在重连'
this.initWebSocket()
},
websocketonmessage(e){
let _this = this //数据接收
console.log('_this',e.data)
_this.text += `数据返回:${e.data}`
// 目前不确定是需要引入store
// if (e.data == '连接成功') {//这个判断是我业务需求才加的
// return
// }
},
websocketsend(Data){//数据发送
this.websock.send(Data)
},
websocketclose(e){ //关闭
console.log('断开连接', e)
},
setInterval() {
this.flag = setInterval(() => {
this.threadPoxi()
},3000)
},
noInterval() {
clearInterval(this.flag)
}
},
mounted() {
// this.set()
},
created() {
this.initWebSocket()
},
destroyed() {
// 清除ws
this.noInterval()
},
}