blob: 9c49d633bcd0acf2db47e82e178f051489f9181f [file] [log] [blame]
刘洪青6266f992017-05-15 21:21:03 +08001<!--
2 Licensed to the Apache Software Foundation (ASF) under one or more
3 contributor license agreements. See the NOTICE file distributed with
4 this work for additional information regarding copyright ownership.
5 The ASF licenses this file to You under the Apache License, Version 2.0
6 (the "License"); you may not use this file except in compliance with
7 the License. You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16-->
17<!DOCTYPE html>
18<html>
19<head>
20 <title>Apache Tomcat WebSocket Examples: Chat</title>
21 <style type="text/css">
22 input#chat {
23 width: 410px
24 }
25
26 #console-container {
27 width: 400px;
28 }
29
30 #console {
31 border: 1px solid #CCCCCC;
32 border-right-color: #999999;
33 border-bottom-color: #999999;
34 height: 170px;
35 overflow-y: scroll;
36 padding: 5px;
37 width: 100%;
38 }
39
40 #console p {
41 padding: 0;
42 margin: 0;
43 }
44 </style>
45 <script type="text/javascript">
46 var Chat = {};
47
48 Chat.socket = null;
49
50 Chat.connect = (function(host) {
51 if ('WebSocket' in window) {
52 Chat.socket = new WebSocket(host);
53 } else if ('MozWebSocket' in window) {
54 Chat.socket = new MozWebSocket(host);
55 } else {
56 Console.log('Error: WebSocket is not supported by this browser.');
57 return;
58 }
59
60 Chat.socket.onopen = function () {
61 Console.log('Info: WebSocket connection opened.');
62 document.getElementById('chat').onkeydown = function(event) {
63 if (event.keyCode == 13) {
64 Chat.sendMessage();
65 }
66 };
67 };
68
69 Chat.socket.onclose = function () {
70 document.getElementById('chat').onkeydown = null;
71 Console.log('Info: WebSocket closed.');
72 };
73
74 Chat.socket.onmessage = function (message) {
75 Console.log(message.data);
76 };
77 });
78
79 Chat.initialize = function() {
80 if (window.location.protocol == 'http:') {
81 Chat.connect('ws://' + window.location.host + '/examples/websocket/tc7/chat');
82 } else {
83 Chat.connect('wss://' + window.location.host + '/examples/websocket/tc7/chat');
84 }
85 };
86
87 Chat.sendMessage = (function() {
88 var message = document.getElementById('chat').value;
89 if (message != '') {
90 Chat.socket.send(message);
91 document.getElementById('chat').value = '';
92 }
93 });
94
95 var Console = {};
96
97 Console.log = (function(message) {
98 var console = document.getElementById('console');
99 var p = document.createElement('p');
100 p.style.wordWrap = 'break-word';
101 p.innerHTML = message;
102 console.appendChild(p);
103 while (console.childNodes.length > 25) {
104 console.removeChild(console.firstChild);
105 }
106 console.scrollTop = console.scrollHeight;
107 });
108
109 Chat.initialize();
110
111 </script>
112</head>
113<body>
114<noscript><h2 style="color: #ff0000">Seems your browser doesn't support Javascript! Websockets rely on Javascript being enabled. Please enable
115 Javascript and reload this page!</h2></noscript>
116<div>
117 <p>
118 <input type="text" placeholder="type and press enter to chat" id="chat">
119 </p>
120 <div id="console-container">
121 <div id="console"></div>
122 </div>
123</div>
124</body>
125</html>