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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2106 by torben, Mon Feb 10 21:54:54 2014 UTC revision 2112 by torben, Tue Feb 11 10:02:13 2014 UTC
# Line 5  Line 5 
5    
6  <script type="text/javascript">  <script type="text/javascript">
7    
8    var actTime = 0;
9    var socket = null;
10    
11    function setActTime() {
12            actTime = new Date().getTime();
13    }
14    
15  function append(str) {  function append(str) {
16            var d = new Date();
17            var dateStr = "[" + d.toLocaleTimeString() + "] ";
18          var log = $("#log");          var log = $("#log");
19          log.val( log.val() + str + "\n")          log.val( log.val() + dateStr + str + "\n")
20    }
21    
22    function sendMessage() {
23            try {
24                    var txt = $("#txt").val();
25                    append(txt);
26                    
27                    socket.send(txt);
28                    
29                    $("#txt").val("");
30                    setActTime();
31                    
32            } catch(exception) {
33                    append(exception);
34            }
35  }  }
36    
37  $( function() {  $( function() {
# Line 18  $( function() { Line 42  $( function() {
42                  var url = "ws://" + document.location.host + "/Test3/wstest";                  var url = "ws://" + document.location.host + "/Test3/wstest";
43                  append("Opening connection to " + url)                  append("Opening connection to " + url)
44                                    
45                  var socket = new WebSocket(url);                  socket = new WebSocket(url);
46                                    
47                  socket.onopen = function() {                  socket.onopen = function() {
48                      append("Socket has been opened!");                      append("Socket has been opened!");
49                        setActTime();
50                  }                  }
51                                    
52                  socket.onmessage = function(msg){                  socket.onmessage = function(msg){
53                      append(msg); //Awesome!                      append(msg.data); //Awesome!
54                        setActTime();
55                  }                  }
56                    
57                  socket.onclose = function(){                  socket.onclose = function(){
58                  append('Connection closed');                          var now = new Date().getTime();
59                            var elapsed = now - actTime;
60                            elapsed = elapsed / 1000;
61                            
62                    append('Connection closed after ' + elapsed + ' seconds of inactivity');
63          }          }
64          } catch(exception){          } catch(exception){
65          append(exception);          append(exception);
66          }          }
67                    
68          $("#btn").click( function() {          $("#btn").click( function() {
69                  try {                  sendMessage();
70                          var txt = $("#txt").val();          });
71                          append(txt);          
72                                    $(document).keypress(function(e) {
73                          socket.send(txt);                  if(e.which == 13) {
74                                                    sendMessage();
                 } catch(exception) {  
                         append(exception);  
75                  }                  }
76          });          });
77                    
78  });  });
79    
80    
81  </script>  </script>
82    
83  </head>  </head>
# Line 55  $( function() { Line 85  $( function() {
85    
86  <h2>Websocket test chat</h2>  <h2>Websocket test chat</h2>
87    
88  <textarea rows="30" cols="160" id="log"></textarea>  <textarea rows="30" cols="160" id="log" readonly></textarea>
89  <br>  <br>
90  <input size=120  type="text" name="txt" id="txt">  <input size=120  type="text" name="txt" id="txt">
91  <button id="btn">Submit</button>  <button id="btn">Submit</button>
92    
93    <br><br>
94    <font size="2">
95    If using websockets with haproxy look into <code>timeout tunnel</code> option.
96    </font>
97    
98  </body>  </body>
 </html>  
99    </html>

Legend:
Removed from v.2106  
changed lines
  Added in v.2112

  ViewVC Help
Powered by ViewVC 1.1.20