blob: 6153bffa753e79dd69f562ee82939766c309dc91 [file] [log] [blame]
刘洪青6266f992017-05-15 21:21:03 +08001<?xml version="1.0" encoding="UTF-8"?>
2<!--
3 Licensed to the Apache Software Foundation (ASF) under one or more
4 contributor license agreements. See the NOTICE file distributed with
5 this work for additional information regarding copyright ownership.
6 The ASF licenses this file to You under the Apache License, Version 2.0
7 (the "License"); you may not use this file except in compliance with
8 the License. You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17-->
18<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
19<head>
20 <title>Apache Tomcat WebSocket Examples: Echo</title>
21 <style type="text/css"><![CDATA[
22 #connect-container {
23 float: left;
24 width: 400px
25 }
26
27 #connect-container div {
28 padding: 5px;
29 }
30
31 #console-container {
32 float: left;
33 margin-left: 15px;
34 width: 400px;
35 }
36
37 #console {
38 border: 1px solid #CCCCCC;
39 border-right-color: #999999;
40 border-bottom-color: #999999;
41 height: 170px;
42 overflow-y: scroll;
43 padding: 5px;
44 width: 100%;
45 }
46
47 #console p {
48 padding: 0;
49 margin: 0;
50 }
51 ]]></style>
52 <script type="application/javascript"><![CDATA[
53 var ws = null;
54
55 function setConnected(connected) {
56 document.getElementById('connect').disabled = connected;
57 document.getElementById('disconnect').disabled = !connected;
58 document.getElementById('echo').disabled = !connected;
59 }
60
61 function connect() {
62 var target = document.getElementById('target').value;
63 if (target == '') {
64 alert('Please select server side connection implementation.');
65 return;
66 }
67 if ('WebSocket' in window) {
68 ws = new WebSocket(target);
69 } else if ('MozWebSocket' in window) {
70 ws = new MozWebSocket(target);
71 } else {
72 alert('WebSocket is not supported by this browser.');
73 return;
74 }
75 ws.onopen = function () {
76 setConnected(true);
77 log('Info: WebSocket connection opened.');
78 };
79 ws.onmessage = function (event) {
80 log('Received: ' + event.data);
81 };
82 ws.onclose = function (event) {
83 setConnected(false);
84 log('Info: WebSocket connection closed, Code: ' + event.code + (event.reason == "" ? "" : ", Reason: " + event.reason));
85 };
86 }
87
88 function disconnect() {
89 if (ws != null) {
90 ws.close();
91 ws = null;
92 }
93 setConnected(false);
94 }
95
96 function echo() {
97 if (ws != null) {
98 var message = document.getElementById('message').value;
99 log('Sent: ' + message);
100 ws.send(message);
101 } else {
102 alert('WebSocket connection not established, please connect.');
103 }
104 }
105
106 function updateTarget(target) {
107 if (window.location.protocol == 'http:') {
108 document.getElementById('target').value = 'ws://' + window.location.host + target;
109 } else {
110 document.getElementById('target').value = 'wss://' + window.location.host + target;
111 }
112 }
113
114 function log(message) {
115 var console = document.getElementById('console');
116 var p = document.createElement('p');
117 p.style.wordWrap = 'break-word';
118 p.appendChild(document.createTextNode(message));
119 console.appendChild(p);
120 while (console.childNodes.length > 25) {
121 console.removeChild(console.firstChild);
122 }
123 console.scrollTop = console.scrollHeight;
124 }
125
126
127 document.addEventListener("DOMContentLoaded", function() {
128 // Remove elements with "noscript" class - <noscript> is not allowed in XHTML
129 var noscripts = document.getElementsByClassName("noscript");
130 for (var i = 0; i < noscripts.length; i++) {
131 noscripts[i].parentNode.removeChild(noscripts[i]);
132 }
133 }, false);
134 ]]></script>
135</head>
136<body>
137<div class="noscript"><h2 style="color: #ff0000">Seems your browser doesn't support Javascript! Websockets rely on Javascript being enabled. Please enable
138 Javascript and reload this page!</h2></div>
139<div>
140 <div id="connect-container">
141 <div>
142 <span>Connect to service implemented using:</span>
143 <!-- echo example using new programmatic API on the server side -->
144 <input id="radio1" type="radio" name="group1" value="/examples/websocket/echoProgrammatic"
145 onclick="updateTarget(this.value);"/> <label for="radio1">programmatic API</label>
146 <!-- echo example using new annotation API on the server side -->
147 <input id="radio2" type="radio" name="group1" value="/examples/websocket/echoAnnotation"
148 onclick="updateTarget(this.value);"/> <label for="radio2">annotation API</label>
149 </div>
150 <div>
151 <input id="target" type="text" size="40" style="width: 350px"/>
152 </div>
153 <div>
154 <button id="connect" onclick="connect();">Connect</button>
155 <button id="disconnect" disabled="disabled" onclick="disconnect();">Disconnect</button>
156 </div>
157 <div>
158 <textarea id="message" style="width: 350px">Here is a message!</textarea>
159 </div>
160 <div>
161 <button id="echo" onclick="echo();" disabled="disabled">Echo message</button>
162 </div>
163 </div>
164 <div id="console-container">
165 <div id="console"/>
166 </div>
167</div>
168</body>
169</html>