View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  //
20  
21  //  ========================================================================
22  //  Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd.
23  //  ------------------------------------------------------------------------
24  //  All rights reserved. This program and the accompanying materials
25  //  are made available under the terms of the Eclipse Public License v1.0
26  //  and Apache License v2.0 which accompanies this distribution.
27  //
28  //      The Eclipse Public License is available at
29  //      http://www.eclipse.org/legal/epl-v10.html
30  //
31  //      The Apache License v2.0 is available at
32  //      http://www.opensource.org/licenses/apache2.0.php
33  //
34  //  You may elect to redistribute this code under either of these licenses.
35  //  ========================================================================
36  //
37  
38  package com.acme;
39  
40  import java.io.IOException;
41  import java.util.List;
42  import java.util.ListIterator;
43  import java.util.concurrent.CopyOnWriteArrayList;
44  
45  import javax.servlet.ServletException;
46  import javax.servlet.http.HttpServletRequest;
47  import javax.servlet.http.HttpServletResponse;
48  
49  import org.eclipse.jetty.websocket.api.UpgradeRequest;
50  import org.eclipse.jetty.websocket.api.UpgradeResponse;
51  import org.eclipse.jetty.websocket.api.WebSocketConnection;
52  import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose;
53  import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
54  import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
55  import org.eclipse.jetty.websocket.api.annotations.WebSocket;
56  import org.eclipse.jetty.websocket.servlet.WebSocketCreator;
57  import org.eclipse.jetty.websocket.servlet.WebSocketServlet;
58  import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory;
59  
60  @SuppressWarnings("serial")
61  public class WebSocketChatServlet extends WebSocketServlet implements WebSocketCreator
62  {
63      /** Holds active sockets to other members of the chat */
64      private final List<ChatWebSocket> members = new CopyOnWriteArrayList<ChatWebSocket>();
65  
66      @Override
67      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
68      {
69          getServletContext().getNamedDispatcher("default").forward(request,response);
70      }
71  
72      @Override
73      public Object createWebSocket(UpgradeRequest req, UpgradeResponse resp)
74      {
75          return new ChatWebSocket();
76      }
77  
78      @Override
79      public void configure(WebSocketServletFactory factory)
80      {
81          factory.register(ChatWebSocket.class);
82          factory.setCreator(this);
83      }
84  
85      /* ------------------------------------------------------------ */
86      /**
87       * Create a WebSocket that echo's back the message to all other members of the servlet.
88       */
89      @WebSocket
90      public class ChatWebSocket
91      {
92          volatile WebSocketConnection connection;
93  
94          @OnWebSocketConnect
95          public void onOpen(WebSocketConnection conn)
96          {
97              connection = conn;
98              members.add(this);
99          }
100 
101         @OnWebSocketMessage
102         public void onMessage(String data)
103         {
104             if (data.contains("disconnect"))
105             {
106                 try
107                 {
108                     connection.close();
109                 }
110                 catch (IOException ignore)
111                 {
112                     // ignore
113                 }
114                 return;
115             }
116 
117             ListIterator<ChatWebSocket> iter = members.listIterator();
118             while (iter.hasNext())
119             {
120                 ChatWebSocket member = iter.next();
121 
122                 // Test if member is now disconnected
123                 if (!member.connection.isOpen())
124                 {
125                     iter.remove();
126                     continue;
127                 }
128 
129                 // Async write the message back.
130                 member.connection.write(data);
131             }
132         }
133 
134         @OnWebSocketClose
135         public void onClose(int code, String message)
136         {
137             members.remove(this);
138         }
139     }
140 }