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.data); //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 |
$("#txt").val(""); |
46 |
|
47 |
} catch(exception) { |
48 |
append(exception); |
49 |
} |
50 |
}); |
51 |
|
52 |
$(document).keypress(function(e) { |
53 |
if(e.which == 13) { |
54 |
try { |
55 |
var txt = $("#txt").val(); |
56 |
append(txt); |
57 |
|
58 |
socket.send(txt); |
59 |
$("#txt").val(""); |
60 |
|
61 |
} catch(exception) { |
62 |
append(exception); |
63 |
} |
64 |
} |
65 |
}); |
66 |
|
67 |
}); |
68 |
|
69 |
|
70 |
</script> |
71 |
|
72 |
</head> |
73 |
<body> |
74 |
|
75 |
<h2>Websocket test chat</h2> |
76 |
|
77 |
<textarea rows="30" cols="160" id="log" readonly></textarea> |
78 |
<br> |
79 |
<input size=120 type="text" name="txt" id="txt"> |
80 |
<button id="btn">Submit</button> |
81 |
|
82 |
|
83 |
</body> |
84 |
</html> |