Repository navigation
Expand file tree
/
Copy pathindex.html
More file actions
76 lines (59 loc) · 1.71 KB
/
Copy pathindex.html
File metadata and controls
76 lines (59 loc) · 1.71 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
<html>
<title>GU Websocket Example</title>
<head>
<script>
// Check if the browser supports Websockets
if (window.WebSocket){
console.log("This browser supports websockets.");
} else {
console.log("This browser does not support websockets.");
}
// Define the host
var host = 'ws://staging.socket.gooduncle.com';
// Establish a connection
var ws = new WebSocket(host);
// Bind to the onopen event if needed
ws.onopen = function(e){
console.log('The socket is open');
};
// Bind to any errors experienes
ws.onerror = function(e){
console.log('The socket is experiencing an error');
};
// Bind to the onclose event if needed
// Note: Dont depend on this method, its unreliable.
ws.onclose = function(e){
console.log('The socket is closed');
};
// Send a single unicode character every 300ms
var _keepAlive = function(){
ws.send('`');
};
var foo = setInterval(_keepAlive, 300);
// Sample logic for detecting faulty connections
var lastReceivedMessage = Date.now();
// Bind to the onmessage event
ws.onmessage = function(e) {
console.log('Received message',e.data);
lastReceivedMessage = Date.now();
};
// Every second, check if the connection is faulty.
var _loop = function(){
var elapsed = Date.now() - lastReceivedMessage;
if(elapsed > 3000){
console.log("The connection is probably dead");
} else {
if(elapsed > 1000){
console.log("The connection is faulty");
} else {
console.log("Everythings fine");
}
}
};
var bar = setInterval(_loop, 1000);
</script>
</head>
<body>
<p>Open the inspector and view the console logs.</p>
</body>
</html>