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 @@ - +
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 @@ wsEchoStream - websocket.EchoStream + websocket.echo.EchoStream wsEchoStream @@ -358,7 +358,7 @@ wsEchoMessage - websocket.EchoMessage + websocket.echo.EchoMessage @@ -376,6 +376,14 @@ wsEchoMessage /websocket/echoMessage + + + wsChat + websocket.chat.ChatWebSocketServlet + + + wsChat + /websocket/chat wsSnake 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 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 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 connections = + new CopyOnWriteArraySet(); + + @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 @@ - +
\ 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 @@ + + + + + Apache Tomcat WebSocket Examples: Chat + + + + + +
+

+ +

+
+
+
+
+ + \ 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. --> -Apache Tomcat WebSocket Examples - - - -

-

Apache Tomcat WebSocket Examples

-

+ + + + Apache Tomcat WebSocket Examples + + +

Apache Tomcat WebSocket Examples

- \ No newline at end of file + + \ No newline at end of file