-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebsocket.html
More file actions
65 lines (55 loc) · 1.82 KB
/
websocket.html
File metadata and controls
65 lines (55 loc) · 1.82 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
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
var MESSAGE = "Hello Mr. Server !"
var start = new Date().getTime()
var ready = function () {
if(!("WebSocket" in window)){
document.write('<p>Oh no, you need a browser that supports WebSockets. How about <a href="http://www.,mozilla.org/firefox">Mozilla Firefox</a>?</p>');
}else {
//The user has WebSockets
var console = document.getElementById('console');
function connect() {
var socket;
var host = "ws://localhost:8080/";
try{
socket = new WebSocket(host);
message('Socket Status: '+socket.readyState);
socket.onopen = function(){
message('Socket Status: '+socket.readyState+' (Open)');
socket.send(MESSAGE);
}
socket.onmessage = function(msg){
end = new Date().getTime()
time = (end - start)
update(msg.data)
socket.send(MESSAGE);
start = new Date().getTime()
}
socket.onclose = function(){
message('Status: '+socket.readyState+' (Closed)');
}
} catch(exception){
message('Error: '+exception);
}
function sendHeartBeat(){
socket.send("Hello Mr. Server!");
}
function update(data) {
console.innerHTML = data
}
function message(msg){
document.innerHTML += "<p>" + msg + "</p>"
}
}
connect();
}
};
</script>
<title>WebSockets Client</title>
</head>
<body onload="ready();" >
<pre id="console" style="background-color:black; color: #00FF33; font-family: Courier" ></pre>
</body>
</html>