forked from sannremy/PHP-Push-WebSocket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
front.html
55 lines (49 loc) · 1.84 KB
/
front.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
<!DOCTYPE html>
<html>
<head>
<title>Front test - PHP Push WebSocket</title>
<meta charset="utf-8" />
<script type="text/javascript">
var ws, url = 'ws://127.0.0.1:5001';
window.onbeforeunload = function() {
ws.send('quit');
};
window.onload = function() {
try {
ws = new WebSocket(url);
write('Connecting... (readyState '+ws.readyState+')');
ws.onopen = function(msg) {
write('Connection successfully opened (readyState ' + this.readyState+')');
};
ws.onmessage = function(msg) {
write('Server says: '+msg.data);
};
ws.onclose = function(msg) {
if(this.readyState == 2)
write('Closing... The connection is going throught the closing handshake (readyState '+this.readyState+')');
else if(this.readyState == 3)
write('Connection closed... The connection has been closed or could not be opened (readyState '+this.readyState+')');
else
write('Connection closed... (unhandled readyState '+this.readyState+')');
};
ws.onerror = function(event) {
terminal.innerHTML = '<li style="color: red;">'+event.data+'</li>'+terminal.innerHTML;
};
}
catch(exception) {
write(exception);
}
};
function write(text) {
var date = new Date();
var dateText = '['+date.getFullYear()+'-'+(date.getMonth()+1 > 9 ? date.getMonth()+1 : '0'+date.getMonth()+1)+'-'+(date.getDate() > 9 ? date.getDate() : '0'+date.getDate())+' '+(date.getHours() > 9 ? date.getHours() : '0'+date.getHours())+':'+(date.getMinutes() > 9 ? date.getMinutes() : '0'+date.getMinutes())+':'+(date.getSeconds() > 9 ? date.getSeconds() : '0'+date.getSeconds())+']';
var terminal = document.getElementById('terminal');
terminal.innerHTML = '<li>'+dateText+' '+text+'</li>'+terminal.innerHTML;
}
</script>
</head>
<body>
<a href="front.html" target="_blank">Add another client</a>
<ul id="terminal"></ul>
</body>
</html>