1 |
torben |
2106 |
<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 |
torben |
2109 |
var actTime = 0; |
9 |
|
|
var socket = null; |
10 |
|
|
|
11 |
|
|
function setActTime() { |
12 |
|
|
actTime = new Date().getTime(); |
13 |
|
|
} |
14 |
|
|
|
15 |
torben |
2106 |
function append(str) { |
16 |
torben |
2112 |
var d = new Date(); |
17 |
|
|
var dateStr = "[" + d.toLocaleTimeString() + "] "; |
18 |
torben |
2106 |
var log = $("#log"); |
19 |
torben |
2112 |
log.val( log.val() + dateStr + str + "\n") |
20 |
torben |
2106 |
} |
21 |
|
|
|
22 |
torben |
2109 |
function sendMessage() { |
23 |
|
|
try { |
24 |
|
|
var txt = $("#txt").val(); |
25 |
torben |
2114 |
txt = $.trim(txt); |
26 |
torben |
2113 |
//append(txt); |
27 |
torben |
2114 |
if (txt == "") |
28 |
|
|
return; |
29 |
torben |
2109 |
|
30 |
torben |
2113 |
socket.send( "CHAT#" + txt); |
31 |
torben |
2109 |
|
32 |
|
|
$("#txt").val(""); |
33 |
|
|
setActTime(); |
34 |
|
|
|
35 |
|
|
} catch(exception) { |
36 |
|
|
append(exception); |
37 |
|
|
} |
38 |
|
|
} |
39 |
|
|
|
40 |
torben |
2106 |
$( function() { |
41 |
|
|
$("#log").val(""); |
42 |
|
|
append("starting app"); |
43 |
|
|
|
44 |
|
|
try { |
45 |
|
|
var url = "ws://" + document.location.host + "/Test3/wstest"; |
46 |
|
|
append("Opening connection to " + url) |
47 |
|
|
|
48 |
torben |
2109 |
socket = new WebSocket(url); |
49 |
torben |
2106 |
|
50 |
|
|
socket.onopen = function() { |
51 |
|
|
append("Socket has been opened!"); |
52 |
torben |
2109 |
setActTime(); |
53 |
torben |
2106 |
} |
54 |
|
|
|
55 |
|
|
socket.onmessage = function(msg){ |
56 |
torben |
2107 |
append(msg.data); //Awesome! |
57 |
torben |
2109 |
setActTime(); |
58 |
torben |
2106 |
} |
59 |
|
|
|
60 |
|
|
socket.onclose = function(){ |
61 |
torben |
2109 |
var now = new Date().getTime(); |
62 |
|
|
var elapsed = now - actTime; |
63 |
torben |
2110 |
elapsed = elapsed / 1000; |
64 |
torben |
2109 |
|
65 |
|
|
append('Connection closed after ' + elapsed + ' seconds of inactivity'); |
66 |
torben |
2106 |
} |
67 |
|
|
} catch(exception){ |
68 |
|
|
append(exception); |
69 |
|
|
} |
70 |
|
|
|
71 |
|
|
$("#btn").click( function() { |
72 |
torben |
2109 |
sendMessage(); |
73 |
torben |
2106 |
}); |
74 |
|
|
|
75 |
torben |
2113 |
$("#btnNick").click( function() { |
76 |
|
|
var nick = $("#nick").val(); |
77 |
|
|
nick = $.trim(nick); |
78 |
|
|
|
79 |
torben |
2114 |
if (nick == "") { |
80 |
|
|
alert("Nick can not be empty"); |
81 |
|
|
return; |
82 |
|
|
} |
83 |
|
|
|
84 |
torben |
2113 |
try { |
85 |
|
|
socket.send( "NICK#" + nick); |
86 |
|
|
} catch(exception) { |
87 |
|
|
append(exception); |
88 |
|
|
} |
89 |
|
|
|
90 |
|
|
}); |
91 |
|
|
|
92 |
torben |
2107 |
$(document).keypress(function(e) { |
93 |
|
|
if(e.which == 13) { |
94 |
torben |
2109 |
sendMessage(); |
95 |
torben |
2107 |
} |
96 |
|
|
}); |
97 |
|
|
|
98 |
torben |
2116 |
$(window).unload(function(){ |
99 |
|
|
socket.close(); |
100 |
|
|
}); |
101 |
|
|
|
102 |
torben |
2106 |
}); |
103 |
torben |
2107 |
|
104 |
|
|
|
105 |
torben |
2106 |
</script> |
106 |
|
|
|
107 |
|
|
</head> |
108 |
|
|
<body> |
109 |
|
|
|
110 |
|
|
<h2>Websocket test chat</h2> |
111 |
|
|
|
112 |
torben |
2107 |
<textarea rows="30" cols="160" id="log" readonly></textarea> |
113 |
torben |
2106 |
<br> |
114 |
|
|
<input size=120 type="text" name="txt" id="txt"> |
115 |
torben |
2113 |
<button id="btn">Submit</button><br><br> |
116 |
torben |
2106 |
|
117 |
torben |
2113 |
<input size=20 type="text" name="nick" id="nick"> |
118 |
|
|
<button id="btnNick">Set Nick</button><br><br> |
119 |
|
|
|
120 |
torben |
2111 |
<br><br> |
121 |
|
|
<font size="2"> |
122 |
|
|
If using websockets with haproxy look into <code>timeout tunnel</code> option. |
123 |
|
|
</font> |
124 |
torben |
2106 |
|
125 |
|
|
</body> |
126 |
torben |
2110 |
</html> |