/[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 2108 by torben, Tue Feb 11 08:33:52 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.data); //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();
                         var txt = $("#txt").val();  
                         append(txt);  
                           
                         socket.send(txt);  
                           
                         $("#txt").val("");  
                           
                 } catch(exception) {  
                         append(exception);  
                 }  
67          });          });
68                    
69          $(document).keypress(function(e) {          $(document).keypress(function(e) {
70                  if(e.which == 13) {                  if(e.which == 13) {
71                          try {                          sendMessage();
                                 var txt = $("#txt").val();  
                                 append(txt);  
                                   
                                 socket.send(txt);  
                                 $("#txt").val("");  
                                   
                         } catch(exception) {  
                                 append(exception);  
                         }  
72                  }                  }
73          });          });
74                    

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

  ViewVC Help
Powered by ViewVC 1.1.20