Ejemplo n.º 1
0
  @Test
  public void testText() throws Exception {

    final AtomicBoolean connected = new AtomicBoolean(false);

    final ServletContainer container = ServletContainer.Factory.newInstance();

    DeploymentUtils.setupServlet(
        new ServletInfo(
                "websocket",
                WebSocketServlet.class,
                new ImmediateInstanceFactory<Servlet>(
                    new WebSocketServlet(
                        new WebSocketConnectionCallback() {
                          @Override
                          public void onConnect(
                              final WebSocketHttpExchange exchange,
                              final WebSocketChannel channel) {
                            connected.set(true);
                            channel
                                .getReceiveSetter()
                                .set(
                                    new AbstractReceiveListener() {

                                      @Override
                                      protected void onFullTextMessage(
                                          WebSocketChannel channel, BufferedTextMessage message)
                                          throws IOException {
                                        final String string = message.getData();
                                        if (string.equals("hello")) {
                                          WebSockets.sendText("world", channel, null);
                                        } else {
                                          WebSockets.sendText(string, channel, null);
                                        }
                                      }
                                    });
                            channel.resumeReceives();
                          }
                        })))
            .addMapping("/*"));

    final FutureResult latch = new FutureResult();
    WebSocketTestClient client =
        new WebSocketTestClient(
            org.jboss.netty.handler.codec.http.websocketx.WebSocketVersion.V13,
            new URI(
                "ws://"
                    + NetworkUtils.formatPossibleIpv6Address(
                        DefaultServer.getHostAddress("default"))
                    + ":"
                    + DefaultServer.getHostPort("default")
                    + "/servletContext/"));
    client.connect();
    client.send(
        new TextWebSocketFrame(ChannelBuffers.copiedBuffer("hello", US_ASCII)),
        new FrameChecker(TextWebSocketFrame.class, "world".getBytes(US_ASCII), latch));
    latch.getIoFuture().get();
    client.destroy();
  }