-
Notifications
You must be signed in to change notification settings - Fork 118
/
index.html
67 lines (58 loc) · 2.04 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script src="socket.io.js"></script>
<script>
$(function() {
var ping = new io.connect('http://' + window.location.host);
// Establish event handlers
ping.on('disconnect', function() {
ping.socket.reconnect();
});
function getPrintableDate(date) {
return date.getFullYear().toString() + '/' +
(date.getMonth()+1).toString() + '/' +
date.getDate().toString() + ' ' +
date.getHours().toString() + ':' +
date.getMinutes().toString() + ':' +
date.getSeconds().toString() + '.' +
date.getMilliseconds().toString();
}
function encodeDate(date)
{
return [date.getHours(), date.getMinutes(), date.getSeconds(), date.getMilliseconds()];
}
function decodeDate(data)
{
var date = new Date();
return new Date(date.getFullYear(), date.getMonth(), date.getDate(),
data[0], data[1], data[2], data[3]);
}
// Ping
ping.on('pong', function(clientStamp, serverStamp) {
var client = decodeDate(clientStamp);
var server = decodeDate(serverStamp);
var now = new Date();
$('#ping').html('Ping: ' + (now.getTime() - client.getTime()).toString() + ' ms.<br/>' +
'C2S: ' + (server.getTime() - client.getTime()).toString() + ' ms.<br/>'
);
});
function sendPing()
{
/* Send ping event.
Alternatively, you can do following:
ping.emit('ping', {client: encodeDate(new Date()), text:'Hey Server' });
*/
ping.emit('ping', encodeDate(new Date()), 'Hey Server');
setTimeout(sendPing, 5000);
}
sendPing();
});
</script>
</head>
<body>
<h3>Ping!</h3>
<div id="ping"></div>
</body>
</html>