/[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 2114 by torben, Tue Feb 11 12:49:11 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                    txt = $.trim(txt);
26                    //append(txt);
27                    if (txt == "")
28                            return;
29                    
30                    socket.send( "CHAT#" + txt);
31                    
32                    $("#txt").val("");
33                    setActTime();
34                    
35            } catch(exception) {
36                    append(exception);
37            }
38  }  }
39    
40  $( function() {  $( function() {
# Line 18  $( function() { Line 45  $( function() {
45                  var url = "ws://" + document.location.host + "/Test3/wstest";                  var url = "ws://" + document.location.host + "/Test3/wstest";
46                  append("Opening connection to " + url)                  append("Opening connection to " + url)
47                                    
48                  var socket = new WebSocket(url);                  socket = new WebSocket(url);
49                                    
50                  socket.onopen = function() {                  socket.onopen = function() {
51                      append("Socket has been opened!");                      append("Socket has been opened!");
52                        setActTime();
53                  }                  }
54                                    
55                  socket.onmessage = function(msg){                  socket.onmessage = function(msg){
56                      append(msg.data); //Awesome!                      append(msg.data); //Awesome!
57                        setActTime();
58                  }                  }
59                    
60                  socket.onclose = function(){                  socket.onclose = function(){
61                  append('Connection closed');                          var now = new Date().getTime();
62                            var elapsed = now - actTime;
63                            elapsed = elapsed / 1000;
64                            
65                    append('Connection closed after ' + elapsed + ' seconds of inactivity');
66          }          }
67          } catch(exception){          } catch(exception){
68          append(exception);          append(exception);
69          }          }
70                    
71          $("#btn").click( function() {          $("#btn").click( function() {
72                    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 {                  try {
85                          var txt = $("#txt").val();                          socket.send( "NICK#" + nick);                  
                         append(txt);  
                           
                         socket.send(txt);  
                           
                         $("#txt").val("");  
                           
86                  } catch(exception) {                  } catch(exception) {
87                          append(exception);                          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                          try {                          sendMessage();
                                 var txt = $("#txt").val();  
                                 append(txt);  
                                   
                                 socket.send(txt);  
                                 $("#txt").val("");  
                                   
                         } catch(exception) {  
                                 append(exception);  
                         }  
95                  }                  }
96          });          });
97                    
# Line 77  $( function() { Line 108  $( function() {
108  <textarea rows="30" cols="160" id="log" readonly></textarea>  <textarea rows="30" cols="160" id="log" readonly></textarea>
109  <br>  <br>
110  <input size=120  type="text" name="txt" id="txt">  <input size=120  type="text" name="txt" id="txt">
111  <button id="btn">Submit</button>  <button id="btn">Submit</button><br><br>
112    
113    <input size=20  type="text" name="nick" id="nick">
114    <button id="btnNick">Set Nick</button><br><br>
115    
116    <br><br>
117    <font size="2">
118    If using websockets with haproxy look into <code>timeout tunnel</code> option.
119    </font>
120    
121  </body>  </body>
 </html>  
122    </html>

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

  ViewVC Help
Powered by ViewVC 1.1.20