/[projects]/miscJava/Test3/WebContent/WebsockTest.html
ViewVC logotype

Annotation of /miscJava/Test3/WebContent/WebsockTest.html

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2111 - (hide annotations) (download) (as text)
Tue Feb 11 09:52:51 2014 UTC (10 years, 3 months ago) by torben
File MIME type: text/html
File size: 1730 byte(s)
add note on haproxy timeout
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     var log = $("#log");
17     log.val( log.val() + str + "\n")
18     }
19    
20 torben 2109 function sendMessage() {
21     try {
22     var txt = $("#txt").val();
23     append(txt);
24    
25     socket.send(txt);
26    
27     $("#txt").val("");
28     setActTime();
29    
30     } catch(exception) {
31     append(exception);
32     }
33     }
34    
35 torben 2106 $( function() {
36     $("#log").val("");
37     append("starting app");
38    
39     try {
40     var url = "ws://" + document.location.host + "/Test3/wstest";
41     append("Opening connection to " + url)
42    
43 torben 2109 socket = new WebSocket(url);
44 torben 2106
45     socket.onopen = function() {
46     append("Socket has been opened!");
47 torben 2109 setActTime();
48 torben 2106 }
49    
50     socket.onmessage = function(msg){
51 torben 2107 append(msg.data); //Awesome!
52 torben 2109 setActTime();
53 torben 2106 }
54    
55     socket.onclose = function(){
56 torben 2109 var now = new Date().getTime();
57     var elapsed = now - actTime;
58 torben 2110 elapsed = elapsed / 1000;
59 torben 2109
60     append('Connection closed after ' + elapsed + ' seconds of inactivity');
61 torben 2106 }
62     } catch(exception){
63     append(exception);
64     }
65    
66     $("#btn").click( function() {
67 torben 2109 sendMessage();
68 torben 2106 });
69    
70 torben 2107 $(document).keypress(function(e) {
71     if(e.which == 13) {
72 torben 2109 sendMessage();
73 torben 2107 }
74     });
75    
76 torben 2106 });
77 torben 2107
78    
79 torben 2106 </script>
80    
81     </head>
82     <body>
83    
84     <h2>Websocket test chat</h2>
85    
86 torben 2107 <textarea rows="30" cols="160" id="log" readonly></textarea>
87 torben 2106 <br>
88     <input size=120 type="text" name="txt" id="txt">
89     <button id="btn">Submit</button>
90    
91 torben 2111 <br><br>
92     <font size="2">
93     If using websockets with haproxy look into <code>timeout tunnel</code> option.
94     </font>
95 torben 2106
96     </body>
97 torben 2110 </html>

  ViewVC Help
Powered by ViewVC 1.1.20