Exemplo n.º 1
0
 @Override
 public void close(CloseReason closeReason) throws IOException {
   if (closed.compareAndSet(false, true)) {
     try {
       if (closeReason == null) {
         endpoint
             .getInstance()
             .onClose(this, new CloseReason(CloseReason.CloseCodes.NO_STATUS_CODE, null));
       } else {
         endpoint.getInstance().onClose(this, closeReason);
       }
       if (!webSocketChannel.isCloseFrameReceived()) {
         // if we have already recieved a close frame then the close frame handler
         // will deal with sending back the reason message
         if (closeReason == null) {
           webSocketChannel.sendClose();
         } else {
           WebSockets.sendClose(
               new CloseMessage(
                       closeReason.getCloseCode().getCode(), closeReason.getReasonPhrase())
                   .toByteBuffer(),
               webSocketChannel,
               null);
         }
       }
     } finally {
       close0();
     }
   }
 }
  @Test
  public void testTextMessage() throws Exception {

    final WebSocketChannel webSocketChannel =
        WebSocketClient.connect(
                worker,
                buffer,
                OptionMap.EMPTY,
                new URI(DefaultServer.getDefaultServerURL()),
                WebSocketVersion.V13)
            .get();

    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<String> result = new AtomicReference<String>();
    webSocketChannel
        .getReceiveSetter()
        .set(
            new ChannelListener<WebSocketChannel>() {
              @Override
              public void handleEvent(final WebSocketChannel channel) {
                ChannelInputStream stream = null;
                try {
                  final StreamSourceFrameChannel r = channel.receive();
                  if (r != null) {
                    stream = new ChannelInputStream(r);
                    result.set(FileUtils.readFile(stream));
                    latch.countDown();
                  }
                } catch (IOException e) {
                  e.printStackTrace();
                  latch.countDown();
                } finally {
                  IoUtils.safeClose(stream);
                }
              }
            });
    webSocketChannel.resumeReceives();

    StreamSinkFrameChannel sendChannel = webSocketChannel.send(WebSocketFrameType.TEXT, 11);
    new StringWriteChannelListener("Hello World").setup(sendChannel);

    latch.await(10, TimeUnit.SECONDS);
    Assert.assertEquals("Hello World", result.get());
    webSocketChannel.sendClose();
  }
Exemplo n.º 3
0
 public UndertowSession(
     WebSocketChannel webSocketChannel,
     URI requestUri,
     Map<String, String> pathParameters,
     Map<String, List<String>> requestParameterMap,
     EndpointSessionHandler handler,
     Principal user,
     InstanceHandle<Endpoint> endpoint,
     EndpointConfig config,
     final String queryString,
     final Encoding encoding,
     final Set<Session> openSessions) {
   this.webSocketChannel = webSocketChannel;
   this.queryString = queryString;
   this.encoding = encoding;
   this.openSessions = openSessions;
   container = handler.getContainer();
   this.user = user;
   this.requestUri = requestUri;
   this.requestParameterMap = Collections.unmodifiableMap(requestParameterMap);
   this.pathParameters = Collections.unmodifiableMap(pathParameters);
   remote = new WebSocketSessionRemoteEndpoint(webSocketChannel, config, encoding);
   this.endpoint = endpoint;
   webSocketChannel
       .getCloseSetter()
       .set(
           new ChannelListener<Channel>() {
             @Override
             public void handleEvent(final Channel channel) {
               close0();
             }
           });
   this.frameHandler = new FrameHandler(this, this.endpoint.getInstance());
   webSocketChannel.getReceiveSetter().set(frameHandler);
   this.sessionId = new SecureRandomSessionIdGenerator().createSessionId();
   this.attrs =
       Collections.synchronizedMap(new HashMap<String, Object>(config.getUserProperties()));
 }
Exemplo n.º 4
0
 @Override
 public void setMaxIdleTimeout(final long milliseconds) {
   webSocketChannel.setIdleTimeout(milliseconds);
 }
Exemplo n.º 5
0
 @Override
 public long getMaxIdleTimeout() {
   return webSocketChannel.getIdleTimeout();
 }
Exemplo n.º 6
0
 @Override
 public boolean isOpen() {
   return webSocketChannel.isOpen();
 }
Exemplo n.º 7
0
 @Override
 public boolean isSecure() {
   return webSocketChannel.isSecure();
 }
Exemplo n.º 8
0
 @Override
 public String getProtocolVersion() {
   return webSocketChannel.getVersion().toHttpHeaderValue();
 }
Exemplo n.º 9
0
 /**
  * sets the recieve listener This should only be used for annotated endpoints.
  *
  * @param handler The handler
  */
 public void setReceiveListener(final ChannelListener<WebSocketChannel> handler) {
   webSocketChannel.getReceiveSetter().set(handler);
 }