@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(); }
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())); }
/** * 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); }