ASF Bugzilla – Attachment 28457 Details for
Bug 51181
Add support for Web Sockets
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
[patch]
Chat example collision fix and cleanup
chat_patch.patch (text/plain), 16.19 KB, created by
Johno Crawford
on 2012-03-12 21:59:38 UTC
(
hide
)
Description:
Chat example collision fix and cleanup
Filename:
MIME Type:
Creator:
Johno Crawford
Created:
2012-03-12 21:59:38 UTC
Size:
16.19 KB
patch
obsolete
>Index: webapps/examples/WEB-INF/classes/websocket/snake/SnakeWebSocketServlet.java >IDEA additional info: >Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP ><+>UTF-8 >=================================================================== >--- webapps/examples/WEB-INF/classes/websocket/snake/SnakeWebSocketServlet.java (revision 1298531) >+++ webapps/examples/WEB-INF/classes/websocket/snake/SnakeWebSocketServlet.java (revision ) >@@ -98,8 +98,8 @@ > private void broadcast(String message) { > for (SnakeMessageInbound connection : getConnections()) { > try { >- CharBuffer response = CharBuffer.wrap(message); >- connection.getWsOutbound().writeTextMessage(response); >+ CharBuffer buffer = CharBuffer.wrap(message); >+ connection.getWsOutbound().writeTextMessage(buffer); > } catch (IOException ignore) { > // Ignore > } >Index: webapps/examples/websocket/snake.html >IDEA additional info: >Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP ><+>UTF-8 >=================================================================== >--- webapps/examples/websocket/snake.html (revision 1298531) >+++ webapps/examples/websocket/snake.html (revision ) >@@ -51,8 +51,8 @@ > </style> > </head> > <body> >- <noscript><h1>Seems your browser doesn't support Javascript! Websockets rely on Javascript being enabled. Please enable >- Javascript and reload this page!</h1></noscript> >+ <noscript><h2 style="color: #ff0000">Seems your browser doesn't support Javascript! Websockets rely on Javascript being enabled. Please enable >+ Javascript and reload this page!</h2></noscript> > <div style="float: left"> > <canvas id="playground" width="640" height="480"></canvas> > </div> >Index: webapps/examples/WEB-INF/web.xml >IDEA additional info: >Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP ><+>ISO-8859-1 >=================================================================== >--- webapps/examples/WEB-INF/web.xml (revision 1298531) >+++ webapps/examples/WEB-INF/web.xml (revision ) >@@ -350,7 +350,7 @@ > <!-- WebSocket Examples --> > <servlet> > <servlet-name>wsEchoStream</servlet-name> >- <servlet-class>websocket.EchoStream</servlet-class> >+ <servlet-class>websocket.echo.EchoStream</servlet-class> > </servlet> > <servlet-mapping> > <servlet-name>wsEchoStream</servlet-name> >@@ -358,7 +358,7 @@ > </servlet-mapping> > <servlet> > <servlet-name>wsEchoMessage</servlet-name> >- <servlet-class>websocket.EchoMessage</servlet-class> >+ <servlet-class>websocket.echo.EchoMessage</servlet-class> > <!-- Uncomment the following block to increase the default maximum > WebSocket buffer size from 2MB to 20MB which is required for the > Autobahn test suite to pass fully. --> >@@ -376,6 +376,14 @@ > <servlet-mapping> > <servlet-name>wsEchoMessage</servlet-name> > <url-pattern>/websocket/echoMessage</url-pattern> >+ </servlet-mapping> >+ <servlet> >+ <servlet-name>wsChat</servlet-name> >+ <servlet-class>websocket.chat.ChatWebSocketServlet</servlet-class> >+ </servlet> >+ <servlet-mapping> >+ <servlet-name>wsChat</servlet-name> >+ <url-pattern>/websocket/chat</url-pattern> > </servlet-mapping> > <servlet> > <servlet-name>wsSnake</servlet-name> >Index: webapps/examples/WEB-INF/classes/websocket/snake/Snake.java >IDEA additional info: >Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP ><+>UTF-8 >=================================================================== >--- webapps/examples/WEB-INF/classes/websocket/snake/Snake.java (revision 1298531) >+++ webapps/examples/WEB-INF/classes/websocket/snake/Snake.java (revision ) >@@ -93,14 +93,24 @@ > head = nextLocation; > } > >+ handleCollisions(snakes); >+ } >+ >+ private void handleCollisions(Collection<Snake> snakes) { > for (Snake snake : snakes) { >- if (snake.getTail().contains(head)) { >+ boolean headCollision = id != snake.id && snake.getHead().equals(head); >+ boolean tailCollision = snake.getTail().contains(head); >+ if (headCollision || tailCollision) { > kill(); > if (id != snake.id) { > snake.reward(); > } > } > } >+ } >+ >+ public synchronized Location getHead() { >+ return head; > } > > public synchronized Collection<Location> getTail() { >Index: webapps/examples/WEB-INF/classes/websocket/chat/ChatWebSocketServlet.java >IDEA additional info: >Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP ><+>UTF-8 >=================================================================== >--- webapps/examples/WEB-INF/classes/websocket/chat/ChatWebSocketServlet.java (revision ) >+++ webapps/examples/WEB-INF/classes/websocket/chat/ChatWebSocketServlet.java (revision ) >@@ -0,0 +1,99 @@ >+/* >+ * Licensed to the Apache Software Foundation (ASF) under one or more >+ * contributor license agreements. See the NOTICE file distributed with >+ * this work for additional information regarding copyright ownership. >+ * The ASF licenses this file to You under the Apache License, Version 2.0 >+ * (the "License"); you may not use this file except in compliance with >+ * the License. You may obtain a copy of the License at >+ * >+ * http://www.apache.org/licenses/LICENSE-2.0 >+ * >+ * Unless required by applicable law or agreed to in writing, software >+ * distributed under the License is distributed on an "AS IS" BASIS, >+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. >+ * See the License for the specific language governing permissions and >+ * limitations under the License. >+ */ >+package websocket.chat; >+ >+import org.apache.catalina.websocket.MessageInbound; >+import org.apache.catalina.websocket.StreamInbound; >+import org.apache.catalina.websocket.WebSocketServlet; >+import org.apache.catalina.websocket.WsOutbound; >+import util.HTMLFilter; >+ >+import java.io.IOException; >+import java.nio.ByteBuffer; >+import java.nio.CharBuffer; >+import java.util.Set; >+import java.util.concurrent.CopyOnWriteArraySet; >+import java.util.concurrent.atomic.AtomicInteger; >+ >+/** >+ * Example web socket servlet for chat. >+ */ >+public class ChatWebSocketServlet extends WebSocketServlet { >+ >+ private static final long serialVersionUID = 1L; >+ >+ private static final String GUEST_PREFIX = "Guest"; >+ >+ private final AtomicInteger connectionIds = new AtomicInteger(0); >+ private final Set<ChatMessageInbound> connections = >+ new CopyOnWriteArraySet<ChatMessageInbound>(); >+ >+ @Override >+ protected StreamInbound createWebSocketInbound(String subProtocol) { >+ return new ChatMessageInbound(connectionIds.incrementAndGet()); >+ } >+ >+ private final class ChatMessageInbound extends MessageInbound { >+ >+ private final String nickname; >+ >+ private ChatMessageInbound(int id) { >+ this.nickname = GUEST_PREFIX + id; >+ } >+ >+ @Override >+ protected void onOpen(WsOutbound outbound) { >+ connections.add(this); >+ String message = String.format("* %s %s", >+ nickname, "has joined."); >+ broadcast(message); >+ } >+ >+ @Override >+ protected void onClose(int status) { >+ connections.remove(this); >+ String message = String.format("* %s %s", >+ nickname, "has disconnected."); >+ broadcast(message); >+ } >+ >+ @Override >+ protected void onBinaryMessage(ByteBuffer message) throws IOException { >+ throw new UnsupportedOperationException( >+ "Binary message not supported."); >+ } >+ >+ @Override >+ protected void onTextMessage(CharBuffer message) throws IOException { >+ // Never trust the client >+ String filteredMessage = String.format("%s: %s", >+ nickname, HTMLFilter.filter(message.toString())); >+ broadcast(filteredMessage); >+ } >+ >+ private void broadcast(String message) { >+ for (ChatMessageInbound connection : connections) { >+ try { >+ CharBuffer buffer = CharBuffer.wrap(message); >+ connection.getWsOutbound().writeTextMessage(buffer); >+ } catch (IOException ignore) { >+ // Ignore >+ } >+ } >+ } >+ } >+} >Index: webapps/examples/websocket/echo.html >IDEA additional info: >Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP ><+>UTF-8 >=================================================================== >--- webapps/examples/websocket/echo.html (revision 1298531) >+++ webapps/examples/websocket/echo.html (revision ) >@@ -30,7 +30,7 @@ > > #console-container { > float: left; >- padding-left: 20px; >+ margin-left: 15px; > width: 400px; > } > >@@ -121,8 +121,8 @@ > </script> > </head> > <body> >-<noscript><h1>Seems your browser doesn't support Javascript! Websockets rely on Javascript being enabled. Please enable >- Javascript and reload this page!</h1></noscript> >+<noscript><h2 style="color: #ff0000">Seems your browser doesn't support Javascript! Websockets rely on Javascript being enabled. Please enable >+ Javascript and reload this page!</h2></noscript> > <div> > <div id="connect-container"> > <div> >\ No newline at end of file >Index: webapps/examples/WEB-INF/classes/websocket/EchoMessage.java >=================================================================== >--- webapps/examples/WEB-INF/classes/websocket/EchoMessage.java (revision 1298531) >+++ webapps/examples/WEB-INF/classes/websocket/echo/EchoMessage.java (revision ) >@@ -14,7 +14,7 @@ > * See the License for the specific language governing permissions and > * limitations under the License. > */ >-package websocket; >+package websocket.echo; > > import java.io.IOException; > import java.nio.ByteBuffer; >Index: webapps/examples/websocket/chat.html >IDEA additional info: >Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP ><+>UTF-8 >=================================================================== >--- webapps/examples/websocket/chat.html (revision ) >+++ webapps/examples/websocket/chat.html (revision ) >@@ -0,0 +1,121 @@ >+<!-- >+ Licensed to the Apache Software Foundation (ASF) under one or more >+ contributor license agreements. See the NOTICE file distributed with >+ this work for additional information regarding copyright ownership. >+ The ASF licenses this file to You under the Apache License, Version 2.0 >+ (the "License"); you may not use this file except in compliance with >+ the License. You may obtain a copy of the License at >+ >+ http://www.apache.org/licenses/LICENSE-2.0 >+ >+ Unless required by applicable law or agreed to in writing, software >+ distributed under the License is distributed on an "AS IS" BASIS, >+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. >+ See the License for the specific language governing permissions and >+ limitations under the License. >+--> >+<!DOCTYPE html> >+<html> >+<head> >+ <title>Apache Tomcat WebSocket Examples: Chat</title> >+ <style type="text/css"> >+ input#chat { >+ width: 410px >+ } >+ >+ #console-container { >+ width: 400px; >+ } >+ >+ #console { >+ border: 1px solid #CCCCCC; >+ border-right-color: #999999; >+ border-bottom-color: #999999; >+ height: 170px; >+ overflow-y: scroll; >+ padding: 5px; >+ width: 100%; >+ } >+ >+ #console p { >+ padding: 0; >+ margin: 0; >+ } >+ </style> >+ <script type="text/javascript"> >+ var Chat = {}; >+ >+ Chat.socket = null; >+ >+ Chat.connect = (function(host) { >+ if ('WebSocket' in window) { >+ Chat.socket = new WebSocket(host); >+ } else if ('MozWebSocket' in window) { >+ Chat.socket = new MozWebSocket(host); >+ } else { >+ Console.log('Error: WebSocket is not supported by this browser.'); >+ return; >+ } >+ >+ Chat.socket.onopen = function () { >+ Console.log('Info: WebSocket connection opened.'); >+ document.getElementById('chat').onkeydown = function(event) { >+ if (event.keyCode == 13) { >+ Chat.sendMessage(); >+ } >+ }; >+ }; >+ >+ Chat.socket.onclose = function () { >+ document.getElementById('chat').onkeydown = null; >+ Console.log('Info: WebSocket closed.'); >+ }; >+ >+ Chat.socket.onmessage = function (message) { >+ Console.log(message.data); >+ }; >+ }); >+ >+ Chat.initialize = function() { >+ Chat.connect('ws://' + window.location.host + '/examples/websocket/chat'); >+ }; >+ >+ Chat.sendMessage = (function() { >+ var message = document.getElementById('chat').value; >+ if (message != '') { >+ Chat.socket.send(message); >+ document.getElementById('chat').value = ''; >+ } >+ }); >+ >+ var Console = {}; >+ >+ Console.log = (function(message) { >+ var console = document.getElementById('console'); >+ var p = document.createElement('p'); >+ p.style.wordWrap = 'break-word'; >+ p.innerHTML = message; >+ console.appendChild(p); >+ while (console.childNodes.length > 25) { >+ console.removeChild(console.firstChild); >+ } >+ console.scrollTop = console.scrollHeight; >+ }); >+ >+ Chat.initialize(); >+ >+ </script> >+</head> >+<body> >+<noscript><h2 style="color: #ff0000">Seems your browser doesn't support Javascript! Websockets rely on Javascript being enabled. Please enable >+ Javascript and reload this page!</h2></noscript> >+<div> >+ <p> >+ <input type="text" placeholder="type and press enter to chat" id="chat"> >+ </p> >+ <div id="console-container"> >+ <div id="console"></div> >+ </div> >+</div> >+</body> >+</html> >\ No newline at end of file >Index: webapps/examples/WEB-INF/classes/websocket/EchoStream.java >=================================================================== >--- webapps/examples/WEB-INF/classes/websocket/EchoStream.java (revision 1298531) >+++ webapps/examples/WEB-INF/classes/websocket/echo/EchoStream.java (revision ) >@@ -14,7 +14,7 @@ > * See the License for the specific language governing permissions and > * limitations under the License. > */ >-package websocket; >+package websocket.echo; > > import java.io.IOException; > import java.io.InputStream; >Index: webapps/examples/websocket/index.html >IDEA additional info: >Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP ><+>UTF-8 >=================================================================== >--- webapps/examples/websocket/index.html (revision 1298531) >+++ webapps/examples/websocket/index.html (revision ) >@@ -15,15 +15,17 @@ > limitations under the License. > --> > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> >-<HTML><HEAD><TITLE>Apache Tomcat WebSocket Examples</TITLE> >-<META http-equiv=Content-Type content="text/html"> >-</HEAD> >-<BODY> >-<P> >-<H3>Apache Tomcat WebSocket Examples</H3> >-<P></P> >+<html> >+<head> >+ <meta http-equiv=Content-Type content="text/html"> >+ <title>Apache Tomcat WebSocket Examples</title> >+</head> >+<body> >+<h3>Apache Tomcat WebSocket Examples</h3> > <ul> >-<li><a href="echo.html">Echo example</a></li> >+ <li><a href="echo.html">Echo example</a></li> >+ <li><a href="chat.html">Chat example</a></li> >-<li><a href="snake.html">Multiplayer snake example</a></li> >+ <li><a href="snake.html">Multiplayer snake example</a></li> > </ul> >-</BODY></HTML> >\ No newline at end of file >+</body> >+</html> >\ No newline at end of file
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 51181
:
28264
|
28342
|
28396
|
28407
|
28408
|
28410
|
28411
|
28415
|
28416
|
28420
|
28426
| 28457