/[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 2107 by torben, Tue Feb 11 08:33:52 2014 UTC revision 2113 by torben, Tue Feb 11 12:44: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( "CHAT#" + 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.data); //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                    sendMessage();
70            });
71            
72            $("#btnNick").click( function() {
73                    var nick = $("#nick").val();
74                    nick = $.trim(nick);
75                    
76                  try {                  try {
77                          var txt = $("#txt").val();                          socket.send( "NICK#" + nick);                  
                         append(txt);  
                           
                         socket.send(txt);  
                           
                         $("#txt").val("");  
                           
78                  } catch(exception) {                  } catch(exception) {
79                          append(exception);                          append(exception);
80                  }                  }
81                    
82          });          });
83                    
84          $(document).keypress(function(e) {          $(document).keypress(function(e) {
85                  if(e.which == 13) {                  if(e.which == 13) {
86                          try {                          sendMessage();
                                 var txt = $("#txt").val();  
                                 append(txt);  
                                   
                                 socket.send(txt);  
                                 $("#txt").val("");  
                                   
                         } catch(exception) {  
                                 append(exception);  
                         }  
87                  }                  }
88          });          });
89                    
# Line 77  $( function() { Line 100  $( function() {
100  <textarea rows="30" cols="160" id="log" readonly></textarea>  <textarea rows="30" cols="160" id="log" readonly></textarea>
101  <br>  <br>
102  <input size=120  type="text" name="txt" id="txt">  <input size=120  type="text" name="txt" id="txt">
103  <button id="btn">Submit</button>  <button id="btn">Submit</button><br><br>
104    
105    <input size=20  type="text" name="nick" id="nick">
106    <button id="btnNick">Set Nick</button><br><br>
107    
108    <br><br>
109    <font size="2">
110    If using websockets with haproxy look into <code>timeout tunnel</code> option.
111    </font>
112    
113  </body>  </body>
 </html>  
114    </html>

Legend:
Removed from v.2107  
changed lines
  Added in v.2113

  ViewVC Help
Powered by ViewVC 1.1.20