-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebscoket.js
83 lines (79 loc) · 2.33 KB
/
webscoket.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
// demo
// webSc.setHost("ws://......");
// webSc.init(function(data){
// alert("server send:"+data);
// })
// webSc.send(" hello ");
// webSc.reStart()
// webSc.close()
var webSc = {
host:"ws://localhost:9502",
interval:'1000',
initPower:0,
ws:null,
closePower:1,
HB:null,
runServer:null,
setHost:function(host){
this.host = host;
},
setIntervalTime:function(interval){
this.interval = interval*1000;
},
init:function(runCallback){
let self = this;
self.ws = new WebSocket(self.host);
self.ws.onopen = function(evt) {
console.log("Connection open ...");
self.HB = setInterval(function(){
if(self.closePower==1){
self.ws.send("heartbeat");
}else{
clearInterval(self.Hb);
}
},self.interval);
self.initPower=1;
};
self.ws.onmessage = function(evt) {
console.log("Received Message: " + evt.data);
if(typeof runCallback == "function"){
self.runServer = runCallback;
runCallback(evt.data);
}
};
self.ws.onclose = function(evt) {
console.log(self);
if(self.closePower == 1){
self.init(runCallback);
}
self.initPower = 0;
};
},
close:function(){
let self = this;
if(self.ws){
self.initPower = 0;
self.closePower = 0;
self.ws.close();
clearInterval(self.HB);
}
},
reStart:function(){
let self = this;
self.close();
setTimeout(function(){
self.closePower = 1;
self.initPower = 1;
self.init(self.runServer);
},500);
return true;
},
send:function(data){
let self = this;
if(self.initPower==1){
self.ws.send(data);
}else{
alert("please run init function");
}
}
}