Guest User

TomcatWebSocketTest TestWebSocketServlet.java

a guest
Jul 6th, 2013
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.56 KB | None | 0 0
  1. package test;
  2.  
  3. import java.io.IOException;
  4. import java.nio.ByteBuffer;
  5. import java.nio.CharBuffer;
  6. import java.util.Set;
  7. import java.util.concurrent.CopyOnWriteArraySet;
  8. import java.util.concurrent.atomic.AtomicInteger;
  9.  
  10. import javax.servlet.http.HttpServletRequest;
  11.  
  12. import org.apache.catalina.websocket.MessageInbound;
  13. import org.apache.catalina.websocket.StreamInbound;
  14. import org.apache.catalina.websocket.WebSocketServlet;
  15. import org.apache.catalina.websocket.WsOutbound;
  16.  
  17. /**
  18.  * Tested with Tomcat 7.0.42
  19.  *
  20.  */
  21. public class TestWebSocketServlet extends WebSocketServlet {
  22.  
  23.     private static final long serialVersionUID = -7663708549630020769L;
  24.  
  25.     // for message that will be sent to client
  26.     private final AtomicInteger _cnt = new AtomicInteger(0);
  27.     // hold each connection in this Set
  28.     private final Set<TestMessageInbound> connections =
  29.         new CopyOnWriteArraySet<TestMessageInbound>();
  30.     /**
  31.      * For create connection only, each connection will
  32.      * handle it self as needed
  33.      */
  34.     @Override
  35.     protected StreamInbound createWebSocketInbound(String subProtocol,
  36.             HttpServletRequest request) {
  37.         /* enable this fragment to send message by server directly
  38.          *
  39.         if (_cnt.getAndIncrement() == 0) {
  40.             new Thread(new Runnable(){
  41.                 public void run () {
  42.                     try{
  43.                         while (true) {
  44.                             send();
  45.                             Thread.sleep(1000);
  46.                         }
  47.                     } catch (Exception e) {
  48.                         e.printStackTrace();
  49.                     }
  50.                 }
  51.             }).start();
  52.         }
  53.         */
  54.         return new TestMessageInbound();
  55.     }
  56.     private final class TestMessageInbound extends MessageInbound {
  57.         // add self instance into connections Set while opened
  58.         @Override
  59.         protected void onOpen(WsOutbound outbound) {
  60.             connections.add(this);
  61.         }
  62.         // remove self instance from connections Set whild closed
  63.         @Override
  64.         protected void onClose(int status) {
  65.             connections.remove(this);
  66.         }
  67.         // ignore binary message since we just want to process text messages
  68.         @Override
  69.         protected void onBinaryMessage(ByteBuffer message) throws IOException {
  70.             throw new UnsupportedOperationException(
  71.                 "Unsupported: Binary message.");
  72.         }
  73.         // send a message to each connection in connections Set
  74.         // while receive a text message
  75.         @Override
  76.         protected void onTextMessage(CharBuffer message) throws IOException {
  77.             send();
  78.         }
  79.     }
  80.     public void send () {
  81.         String msg = "Current count: " + _cnt.getAndIncrement();
  82.         for (TestMessageInbound connection : connections) {
  83.             try {
  84.                 connection.getWsOutbound().writeTextMessage(CharBuffer.wrap(msg));
  85.             } catch (IOException ignore) {
  86.                 /* ignore */
  87.             }
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment