-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhttp-ping.html
More file actions
138 lines (124 loc) · 3.31 KB
/
http-ping.html
File metadata and controls
138 lines (124 loc) · 3.31 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<link rel="import" href="../polymer/polymer.html">
<!--
# http-ping
Ping http(s) url and retrieve the status and delay
The component use the fetch API, so use it with recent browser.
The component has no GUI.
@demo demo/index.html
-->
<dom-module id="http-ping">
<script>
Polymer({
is: 'http-ping',
properties: {
/**
* The url to ping
* @type {String}
*/
url: {
type: String,
value: 'https://www.google.com',
},
/**
* requests interval in milliseconds
*
* be aware to set an interval bigger that the timeout.
*
* @type {Number}
*/
interval: {
type: Number,
value: 2000,
observer: '_intervalChanged',
},
/**
* Timeout in milliseconds before declaring the url unreachable
* @type {Number}
*/
timeout: {
type: Number,
value: 1000,
},
/**
* Status of the connection
* @type {Boolean}
*/
status: {
type: Boolean,
value: false,
readOnly: true,
notify: true,
reflectToAttribute: true,
},
/**
* Delay of the ping in ms
* @type {String}
*/
delay: {
type: Number,
value: null,
readOnly: true,
notify: true,
reflectToAttribute: true,
},
},
/**
* restart the loop when the value of interval change
* @method _intervalChanged
* @return {none}
*/
_intervalChanged: function() {
if(this._loop) {
clearInterval(this._loop);
}
this._loop = setInterval(this._httpPing.bind(this), this.interval);
},
/**
* do the ping.
*
* @method _httpPing
* @return {none}
*/
_httpPing: function() {
const start = new Date();
let timeout = new Promise((resolve, reject) => {
setTimeout(reject, this.timeout, 'request timed out');
});
let ping = fetch(`${this.url}/?${+ new Date()}`, { mode:'no-cors' } )
.then(response => {
return new Date() - start;
});
Promise.race([timeout, ping])
.then(delay => {
if(!this.status) {
/**
* Fired when the connection state changes.
*
* @event ping-status
* @param {Boolean} status the connection status
*/
this.fire('ping-status', {status: true});
}
this._setStatus(true);
/**
* Fired when a ping is done.
*
* @event ping-delay
* @param {String} url the url to ping.
* @param {Number} delay the delay of the ping.
*/
this.fire('ping-delay', { url: this.url, delay: delay });
this._setDelay(delay);
})
.catch( err => {
if(!this.status) {
this.fire('ping-status', {status: false});
}
this._setStatus(false);
this._setDelay(null);
this.fire('ping-delay', { url: this.url, delay: '' });
})
},
});
</script>
</dom-module>