/[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 2109 by torben, Tue Feb 11 09:32:16 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                            
59                    append('Connection closed after ' + elapsed + ' seconds of inactivity');
60          }          }
61          } catch(exception){          } catch(exception){
62          append(exception);          append(exception);
63          }          }
64                    
65          $("#btn").click( function() {          $("#btn").click( function() {
66                  try {                  sendMessage();
67                          var txt = $("#txt").val();          });
68                          append(txt);          
69                                    $(document).keypress(function(e) {
70                          socket.send(txt);                  if(e.which == 13) {
71                                                    sendMessage();
                 } catch(exception) {  
                         append(exception);  
72                  }                  }
73          });          });
74                    
75  });  });
76    
77    
78  </script>  </script>
79    
80  </head>  </head>
# Line 55  $( function() { Line 82  $( function() {
82    
83  <h2>Websocket test chat</h2>  <h2>Websocket test chat</h2>
84    
85  <textarea rows="30" cols="160" id="log"></textarea>  <textarea rows="30" cols="160" id="log" readonly></textarea>
86  <br>  <br>
87  <input size=120  type="text" name="txt" id="txt">  <input size=120  type="text" name="txt" id="txt">
88  <button id="btn">Submit</button>  <button id="btn">Submit</button>

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

  ViewVC Help
Powered by ViewVC 1.1.20