Author: markt Date: Wed Dec 19 22:24:28 2012 New Revision: 1424176 URL: http://svn.apache.org/viewvc?rev=1424176&view=rev Log: Switch the chat WebSocket example to the new API.
Added: tomcat/trunk/webapps/examples/WEB-INF/classes/websocket/chat/ChatAnnotation.java (with props) Removed: tomcat/trunk/webapps/examples/WEB-INF/classes/websocket/chat/ChatWebSocketServlet.java Modified: tomcat/trunk/webapps/examples/WEB-INF/web.xml Added: tomcat/trunk/webapps/examples/WEB-INF/classes/websocket/chat/ChatAnnotation.java URL: http://svn.apache.org/viewvc/tomcat/trunk/webapps/examples/WEB-INF/classes/websocket/chat/ChatAnnotation.java?rev=1424176&view=auto ============================================================================== --- tomcat/trunk/webapps/examples/WEB-INF/classes/websocket/chat/ChatAnnotation.java (added) +++ tomcat/trunk/webapps/examples/WEB-INF/classes/websocket/chat/ChatAnnotation.java Wed Dec 19 22:24:28 2012 @@ -0,0 +1,92 @@ +/* + * 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 java.io.IOException; +import java.util.Set; +import java.util.concurrent.CopyOnWriteArraySet; +import java.util.concurrent.atomic.AtomicInteger; + +import javax.websocket.Session; +import javax.websocket.WebSocketClose; +import javax.websocket.WebSocketEndpoint; +import javax.websocket.WebSocketMessage; +import javax.websocket.WebSocketOpen; + +import util.HTMLFilter; + +@WebSocketEndpoint(value = "/websocket/chat") +public class ChatAnnotation { + + private static final String GUEST_PREFIX = "Guest"; + private static final AtomicInteger connectionIds = new AtomicInteger(0); + private static final Set<ChatAnnotation> connections = + new CopyOnWriteArraySet<>(); + + private final String nickname; + private Session session; + + public ChatAnnotation() { + nickname = GUEST_PREFIX + connectionIds.getAndIncrement(); + } + + + @WebSocketOpen + public void start(Session session) { + this.session = session; + connections.add(this); + String message = String.format("* %s %s", nickname, "has joined."); + broadcast(message); + } + + + @WebSocketClose + public void end() { + connections.remove(this); + String message = String.format("* %s %s", + nickname, "has disconnected."); + broadcast(message); + } + + + @WebSocketMessage + public void incoming(String message) { + // Never trust the client + String filteredMessage = String.format("%s: %s", + nickname, HTMLFilter.filter(message.toString())); + broadcast(filteredMessage); + } + + + private static void broadcast(String msg) { + for (ChatAnnotation client : connections) { + try { + client.session.getRemote().sendString(msg); + } catch (IOException e) { + connections.remove(client); + try { + client.session.close(); + } catch (IOException e1) { + // Ignore + } + String message = String.format("* %s %s", + client.nickname, "has been disconnected."); + broadcast(message); + } + } + } +} Propchange: tomcat/trunk/webapps/examples/WEB-INF/classes/websocket/chat/ChatAnnotation.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: tomcat/trunk/webapps/examples/WEB-INF/web.xml URL: http://svn.apache.org/viewvc/tomcat/trunk/webapps/examples/WEB-INF/web.xml?rev=1424176&r1=1424175&r2=1424176&view=diff ============================================================================== --- tomcat/trunk/webapps/examples/WEB-INF/web.xml (original) +++ tomcat/trunk/webapps/examples/WEB-INF/web.xml Wed Dec 19 22:24:28 2012 @@ -359,14 +359,6 @@ <listener-class>websocket.echo.WsConfigListener</listener-class> </listener> <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> <servlet-class>websocket.snake.SnakeWebSocketServlet</servlet-class> </servlet> --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org