Skip to content

Commit 50f2518

Browse files
committed
- support ping function, which send ping message to server every 60s by default if page is visible
- fix bug: `status()` should query status from `_src` if this is a scoped ws. - bump version
1 parent 8d6a545 commit 50f2518

File tree

7 files changed

+87
-14
lines changed

7 files changed

+87
-14
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Change Logs
22

3+
## v0.0.14
4+
5+
- support `ping` function, which send `ping` message to server every 60s by default if page is visible
6+
- fix bug: `status()` should query status from `_src` if this is a scoped ws.
7+
8+
39
## v0.0.13
410

511
- pass `op` parameter from middleware `submit` action to access function for fine-grained access control

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ API (ews):
3535
- `0`: not connected
3636
- `1`: connecting
3737
- `2`: connected
38-
38+
- `ping(opt)`: periodically ping remote server (heartbeat) to prevent disconnecting.
39+
- `opt` is an object with following field:
40+
- `now`: default false. if false, ping message is sent after ping interval;
41+
otherwise it will be sent immediately.
42+
- `unping()`: stop ping.
3943

4044
API (from original WebSocket):
4145

dist/index.js

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,51 @@
6161
canceller: null,
6262
disconnector: null
6363
};
64+
this._ping = {
65+
hdr: null,
66+
interval: 60
67+
};
6468
this._s = 0;
6569
return this;
6670
};
67-
ews.prototype = (ref$ = Object.create(Object.prototype), ref$.addEventListener = function(t, cb, o, fromon){
71+
ews.prototype = (ref$ = Object.create(Object.prototype), ref$.unping = function(){
72+
var ref$;
73+
clearTimeout(this._ping.hdr);
74+
document.removeEventListener('visibilitychange', this._ping.check);
75+
return ref$ = this._ping, ref$.hdr = null, ref$.running = false, ref$;
76+
}, ref$.ping = function(opt){
77+
var ref$, this$ = this;
78+
opt == null && (opt = {});
79+
if (!this._ping.running) {
80+
this._ping.isVisible = function(){
81+
return this$._ping.visible = document.visibilityState === 'visible';
82+
};
83+
this._ping.check = function(){
84+
if (this$._ping.isVisible()) {
85+
return this$.ping({
86+
now: true
87+
});
88+
}
89+
};
90+
this._ping.running = true;
91+
document.addEventListener('visibilitychange', this._ping.check);
92+
}
93+
if (this._ping.hdr) {
94+
clearTimeout(this._ping.hdr);
95+
}
96+
if (!this._ping.isVisible) {
97+
return;
98+
}
99+
if (opt.now && this.status() === 2) {
100+
this.send("ping");
101+
}
102+
return this._ping.hdr = setTimeout(function(){
103+
this$._ping.hdr = null;
104+
return this$.ping({
105+
now: true
106+
});
107+
}, 1000 * ((ref$ = this._ping.interval || 60) > 60 ? ref$ : 60));
108+
}, ref$.addEventListener = function(t, cb, o, fromon){
68109
var ref$;
69110
if (!(t === 'message' || t === 'open' || t === 'close' || t === 'error')) {
70111
return;
@@ -392,7 +433,9 @@
392433
}
393434
};
394435
ref$.status = function(){
395-
return this._s;
436+
return this._src
437+
? this._src.status()
438+
: this._s;
396439
};
397440
ref$.ensure = function(){
398441
if (this._s === 2) {

dist/index.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@plotdb/ews",
3-
"version": "0.0.13",
3+
"version": "0.0.14",
44
"description": "elastic websocket",
55
"main": "dist/index.min.js",
66
"browser": "dist/index.min.js",

src/index.ls

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,32 @@ ews = (o = {}) ->
4343
hdr: null
4444
canceller: null
4545
disconnector: null
46+
# ping controller
47+
_ping: {hdr: null, interval: 60}
4648
# status. 0: disconnected. 1: connecting. 2: connected.
4749
_s: 0
4850
@
4951

5052
# essential websocket APIs
5153
ews.prototype = Object.create(Object.prototype) <<<
54+
unping: ->
55+
clearTimeout @_ping.hdr
56+
document.removeEventListener \visibilitychange, @_ping.check
57+
@_ping <<< hdr: null, running: false
58+
59+
ping: (opt = {}) ->
60+
if !@_ping.running =>
61+
@_ping.is-visible = ~> @_ping.visible = (document.visibilityState == \visible)
62+
@_ping.check = ~> if @_ping.is-visible! => @ping now: true
63+
@_ping.running = true
64+
document.addEventListener \visibilitychange, @_ping.check
65+
if @_ping.hdr => clearTimeout @_ping.hdr
66+
if !@_ping.is-visible => return
67+
if opt.now and @status! == 2 => @send("ping")
68+
@_ping.hdr = setTimeout (~>
69+
@_ping.hdr = null
70+
@ping now: true
71+
), (1000 * ((@_ping.interval or 60) >? 60))
5272

5373
# we may add event listener before ws is created.
5474
# additionally, we may reconnect.
@@ -216,7 +236,7 @@ ews.prototype <<<
216236
@_s = s
217237
if s != os => @fire \status, s
218238

219-
status: -> return @_s
239+
status: -> return if @_src => @_src.status! else @_s
220240
ensure: -> if @_s == 2 => Promise.resolve! else @connect!
221241

222242

0 commit comments

Comments
 (0)