/[projects]/miscJava/Test3/WebContent/WebsockTest.html
ViewVC logotype

Annotation of /miscJava/Test3/WebContent/WebsockTest.html

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2107 - (hide annotations) (download) (as text)
Tue Feb 11 08:33:52 2014 UTC (10 years, 3 months ago) by torben
File MIME type: text/html
File size: 1436 byte(s)
Working annotated websocket example
1 torben 2106 <html>
2     <head>
3     <title>Test3 :: Websocket Test</title>
4     <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
5    
6     <script type="text/javascript">
7    
8     function append(str) {
9     var log = $("#log");
10     log.val( log.val() + str + "\n")
11     }
12    
13     $( function() {
14     $("#log").val("");
15     append("starting app");
16    
17     try {
18     var url = "ws://" + document.location.host + "/Test3/wstest";
19     append("Opening connection to " + url)
20    
21     var socket = new WebSocket(url);
22    
23     socket.onopen = function() {
24     append("Socket has been opened!");
25     }
26    
27     socket.onmessage = function(msg){
28 torben 2107 append(msg.data); //Awesome!
29 torben 2106 }
30    
31     socket.onclose = function(){
32     append('Connection closed');
33     }
34     } catch(exception){
35     append(exception);
36     }
37    
38     $("#btn").click( function() {
39     try {
40     var txt = $("#txt").val();
41     append(txt);
42    
43     socket.send(txt);
44    
45 torben 2107 $("#txt").val("");
46    
47 torben 2106 } catch(exception) {
48     append(exception);
49     }
50     });
51    
52 torben 2107 $(document).keypress(function(e) {
53     if(e.which == 13) {
54     try {
55     var txt = $("#txt").val();
56     append(txt);
57    
58     socket.send(txt);
59     $("#txt").val("");
60    
61     } catch(exception) {
62     append(exception);
63     }
64     }
65     });
66    
67 torben 2106 });
68 torben 2107
69    
70 torben 2106 </script>
71    
72     </head>
73     <body>
74    
75     <h2>Websocket test chat</h2>
76    
77 torben 2107 <textarea rows="30" cols="160" id="log" readonly></textarea>
78 torben 2106 <br>
79     <input size=120 type="text" name="txt" id="txt">
80     <button id="btn">Submit</button>
81    
82    
83     </body>
84     </html>

  ViewVC Help
Powered by ViewVC 1.1.20