1 |
<html> |
2 |
<head> |
3 |
<title>Test3 :: Websocket Test</title> |
4 |
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> |
5 |
|
6 |
<script type="text/javascript"> |
7 |
|
8 |
function append(str) { |
9 |
var log = $("#log"); |
10 |
log.val( log.val() + str + "\n") |
11 |
} |
12 |
|
13 |
$( function() { |
14 |
$("#log").val(""); |
15 |
append("starting app"); |
16 |
|
17 |
try { |
18 |
var url = "ws://" + document.location.host + "/Test3/wstest"; |
19 |
append("Opening connection to " + url) |
20 |
|
21 |
var socket = new WebSocket(url); |
22 |
|
23 |
socket.onopen = function() { |
24 |
append("Socket has been opened!"); |
25 |
} |
26 |
|
27 |
socket.onmessage = function(msg){ |
28 |
append(msg); //Awesome! |
29 |
} |
30 |
|
31 |
socket.onclose = function(){ |
32 |
append('Connection closed'); |
33 |
} |
34 |
} catch(exception){ |
35 |
append(exception); |
36 |
} |
37 |
|
38 |
$("#btn").click( function() { |
39 |
try { |
40 |
var txt = $("#txt").val(); |
41 |
append(txt); |
42 |
|
43 |
socket.send(txt); |
44 |
|
45 |
} catch(exception) { |
46 |
append(exception); |
47 |
} |
48 |
}); |
49 |
|
50 |
}); |
51 |
</script> |
52 |
|
53 |
</head> |
54 |
<body> |
55 |
|
56 |
<h2>Websocket test chat</h2> |
57 |
|
58 |
<textarea rows="30" cols="160" id="log"></textarea> |
59 |
<br> |
60 |
<input size=120 type="text" name="txt" id="txt"> |
61 |
<button id="btn">Submit</button> |
62 |
|
63 |
|
64 |
</body> |
65 |
</html> |