/[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 2110 by torben, Tue Feb 11 09:50:30 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 log = $("#log");          var log = $("#log");
17          log.val( log.val() + str + "\n")          log.val( log.val() + str + "\n")
18  }  }
19    
20    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  $( function() {  $( function() {
36          $("#log").val("");          $("#log").val("");
37          append("starting app");          append("starting app");
# Line 18  $( function() { Line 40  $( function() {
40                  var url = "ws://" + document.location.host + "/Test3/wstest";                  var url = "ws://" + document.location.host + "/Test3/wstest";
41                  append("Opening connection to " + url)                  append("Opening connection to " + url)
42                                    
43                  var socket = new WebSocket(url);                  socket = new WebSocket(url);
44                                    
45                  socket.onopen = function() {                  socket.onopen = function() {
46                      append("Socket has been opened!");                      append("Socket has been opened!");
47                        setActTime();
48                  }                  }
49                                    
50                  socket.onmessage = function(msg){                  socket.onmessage = function(msg){
51                      append(msg); //Awesome!                      append(msg.data); //Awesome!
52                        setActTime();
53                  }                  }
54                    
55                  socket.onclose = function(){                  socket.onclose = function(){
56                  append('Connection closed');                          var now = new Date().getTime();
57                            var elapsed = now - actTime;
58                            elapsed = elapsed / 1000;
59                            
60                    append('Connection closed after ' + elapsed + ' seconds of inactivity');
61          }          }
62          } catch(exception){          } catch(exception){
63          append(exception);          append(exception);
64          }          }
65                    
66          $("#btn").click( function() {          $("#btn").click( function() {
67                  try {                  sendMessage();
68                          var txt = $("#txt").val();          });
69                          append(txt);          
70                                    $(document).keypress(function(e) {
71                          socket.send(txt);                  if(e.which == 13) {
72                                                    sendMessage();
                 } catch(exception) {  
                         append(exception);  
73                  }                  }
74          });          });
75                    
76  });  });
77    
78    
79  </script>  </script>
80    
81  </head>  </head>
# Line 55  $( function() { Line 83  $( function() {
83    
84  <h2>Websocket test chat</h2>  <h2>Websocket test chat</h2>
85    
86  <textarea rows="30" cols="160" id="log"></textarea>  <textarea rows="30" cols="160" id="log" readonly></textarea>
87  <br>  <br>
88  <input size=120  type="text" name="txt" id="txt">  <input size=120  type="text" name="txt" id="txt">
89  <button id="btn">Submit</button>  <button id="btn">Submit</button>
90    
91    
92  </body>  </body>
 </html>  
93    </html>

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

  ViewVC Help
Powered by ViewVC 1.1.20