ws.html
1.37 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
<html>
<head>
<title>Web Sockets Demo</title>
<script src="libs/jquery-2.1.1.min.js"></script>
<script type='text/javascript'>
if (!window.WebSocket)
alert("WebSocket not supported by this browser");
var server = {
connect : function() {
var location = document.location.toString().replace('http://',
'ws://').replace('https://', 'wss://').replace('ws.html','ws/topology');
this.ws = new WebSocket(location);
this.ws.onopen = function() {
server._send("Hi there!");
};
this.ws.onmessage = function(m) {
if (m.data) {
$('#log').append(m.data).append($('<br/>'));
}
};
this.ws.onclose = function(m) {
this.ws = null;
};
},
_send : function(message) {
if (this.ws) {
this.ws.send(message);
}
},
send : function(text) {
if (text != null && text.length > 0) {
server._send(text);
}
}
};
</script>
</head>
<body>
<pre id='log'></pre>
<script type='text/javascript'>
server.connect();
</script>
</body>
</html>