/[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 2118 by torben, Tue Feb 11 16:32: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                    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.onerror = function(evt) {
61                            append("*** Error om client websocket ***");
62                  }                  }
63                    
64                  socket.onclose = function(){                  socket.onclose = function(){
65                  append('Connection closed');                          var now = new Date().getTime();
66                            var elapsed = now - actTime;
67                            elapsed = elapsed / 1000;
68                            
69                    append('Connection closed after ' + elapsed + ' seconds of inactivity');
70          }          }
71          } catch(exception){          } catch(exception){
72          append(exception);          append(exception);
73          }          }
74                    
75          $("#btn").click( function() {          $("#btn").click( function() {
76                    sendMessage();
77            });
78            
79            $("#btnNick").click( function() {
80                    var nick = $("#nick").val();
81                    nick = $.trim(nick);
82                    
83                    if (nick == "") {
84                            alert("Nick can not be empty");
85                            return;
86                    }
87                    
88                  try {                  try {
89                          var txt = $("#txt").val();                          socket.send( "NICK#" + nick);                  
                         append(txt);  
                           
                         socket.send(txt);  
                           
                         $("#txt").val("");  
                           
90                  } catch(exception) {                  } catch(exception) {
91                          append(exception);                          append(exception);
92                  }                  }
93                    
94          });          });
95                    
96          $(document).keypress(function(e) {          $(document).keypress(function(e) {
97                  if(e.which == 13) {                  if(e.which == 13) {
98                          try {                          sendMessage();
                                 var txt = $("#txt").val();  
                                 append(txt);  
                                   
                                 socket.send(txt);  
                                 $("#txt").val("");  
                                   
                         } catch(exception) {  
                                 append(exception);  
                         }  
99                  }                  }
100          });          });
101                    
102            $(window).unload(function(){
103                    socket.close();
104            });
105            
106  });  });
107    
108    
# Line 77  $( function() { Line 116  $( function() {
116  <textarea rows="30" cols="160" id="log" readonly></textarea>  <textarea rows="30" cols="160" id="log" readonly></textarea>
117  <br>  <br>
118  <input size=120  type="text" name="txt" id="txt">  <input size=120  type="text" name="txt" id="txt">
119  <button id="btn">Submit</button>  <button id="btn">Submit</button><br><br>
120    
121    <input size=20  type="text" name="nick" id="nick">
122    <button id="btnNick">Set Nick</button><br><br>
123    
124    <br><br>
125    <font size="2">
126    If using websockets with haproxy look into <code>timeout tunnel</code> option.
127    </font>
128    
129  </body>  </body>
 </html>  
130    </html>

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

  ViewVC Help
Powered by ViewVC 1.1.20