/[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 2109 by torben, Tue Feb 11 09:32:16 2014 UTC revision 2116 by torben, Tue Feb 11 13:02:21 2014 UTC
# Line 13  function setActTime() { Line 13  function setActTime() {
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() {  function sendMessage() {
23          try {          try {
24                  var txt = $("#txt").val();                  var txt = $("#txt").val();
25                  append(txt);                  txt = $.trim(txt);
26                    //append(txt);
27                    if (txt == "")
28                            return;
29                                    
30                  socket.send(txt);                  socket.send( "CHAT#" + txt);
31                                    
32                  $("#txt").val("");                  $("#txt").val("");
33                  setActTime();                  setActTime();
# Line 55  $( function() { Line 60  $( function() {
60                  socket.onclose = function(){                  socket.onclose = function(){
61                          var now = new Date().getTime();                          var now = new Date().getTime();
62                          var elapsed = now - actTime;                          var elapsed = now - actTime;
63                            elapsed = elapsed / 1000;
64                                                    
65                  append('Connection closed after ' + elapsed + ' seconds of inactivity');                  append('Connection closed after ' + elapsed + ' seconds of inactivity');
66          }          }
# Line 66  $( function() { Line 72  $( function() {
72                  sendMessage();                  sendMessage();
73          });          });
74                    
75            $("#btnNick").click( function() {
76                    var nick = $("#nick").val();
77                    nick = $.trim(nick);
78                    
79                    if (nick == "") {
80                            alert("Nick can not be empty");
81                            return;
82                    }
83                    
84                    try {
85                            socket.send( "NICK#" + nick);                  
86                    } catch(exception) {
87                            append(exception);
88                    }
89                    
90            });
91            
92          $(document).keypress(function(e) {          $(document).keypress(function(e) {
93                  if(e.which == 13) {                  if(e.which == 13) {
94                          sendMessage();                          sendMessage();
95                  }                  }
96          });          });
97                    
98            $(window).unload(function(){
99                    socket.close();
100            });
101            
102  });  });
103    
104    
# Line 85  $( function() { Line 112  $( function() {
112  <textarea rows="30" cols="160" id="log" readonly></textarea>  <textarea rows="30" cols="160" id="log" readonly></textarea>
113  <br>  <br>
114  <input size=120  type="text" name="txt" id="txt">  <input size=120  type="text" name="txt" id="txt">
115  <button id="btn">Submit</button>  <button id="btn">Submit</button><br><br>
116    
117    <input size=20  type="text" name="nick" id="nick">
118    <button id="btnNick">Set Nick</button><br><br>
119    
120    <br><br>
121    <font size="2">
122    If using websockets with haproxy look into <code>timeout tunnel</code> option.
123    </font>
124    
125  </body>  </body>
 </html>  
126    </html>

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

  ViewVC Help
Powered by ViewVC 1.1.20