@Override public void onConnect(WebSocketHttpExchange httpExchange, WebSocketChannel channel) { UndertowWebSocketSession session = createSession(channel); UndertowWebSocketHandlerAdapter adapter = new UndertowWebSocketHandlerAdapter(session); channel.getReceiveSetter().set(adapter); channel.resumeReceives(); this.handler.handle(session).subscribe(session); }
@Override public boolean isOpen() { return channel.isOpen(); }
/** * Create a new {@link WebSocketChannel} * 8 * * @param connectedStreamChannel The {@link org.xnio.channels.ConnectedStreamChannel} over which the WebSocket Frames should get send and received. * Be aware that it already must be "upgraded". * @param bufferPool The {@link org.xnio.Pool} which will be used to acquire {@link java.nio.ByteBuffer}'s from. * @param version The {@link WebSocketVersion} of the {@link WebSocketChannel} * @param wsUrl The url for which the channel was created. * @param client * @param peerConnections The concurrent set that is used to track open connections associtated with an endpoint */ protected WebSocketChannel(final StreamConnection connectedStreamChannel, ByteBufferPool bufferPool, WebSocketVersion version, String wsUrl, String subProtocol, final boolean client, boolean extensionsSupported, final ExtensionFunction extensionFunction, Set<WebSocketChannel> peerConnections, OptionMap options) { super(connectedStreamChannel, bufferPool, new WebSocketFramePriority(), null, options); this.client = client; this.version = version; this.wsUrl = wsUrl; this.extensionsSupported = extensionsSupported; this.extensionFunction = extensionFunction; this.hasReservedOpCode = extensionFunction.hasExtensionOpCode(); this.subProtocol = subProtocol; this.peerConnections = peerConnections; addCloseTask(new ChannelListener<WebSocketChannel>() { @Override public void handleEvent(WebSocketChannel channel) { extensionFunction.dispose(); WebSocketChannel.this.peerConnections.remove(WebSocketChannel.this); } }); }
public void connect(final WebSocketChannel channel) { this.channel = channel; this.channel.setIdleTimeout(idleTimeout); this.channel.getReceiveSetter().set(this); this.channel.resumeReceives(); this.onConnectCallback.run(); ready.countDown(); }
logger.log(Level.INFO, channel.toString() + " disconnected due to invalid origin header: " + originHeader); exchange.close(); logger.log(Level.INFO, "Valid origin header, setting up connection."); channel.getReceiveSetter().set(new AbstractReceiveListener() { @Override protected void onFullTextMessage(WebSocketChannel clientChannel, BufferedTextMessage message) { daemonWebServer.addClientChannel(channel, requestFormatType); channel.resumeReceives();
@Override public void resume() { channel.resumeReceives(); }
if (endpoint != null) { endpoint.onOpen(channel, exchange); channel.addCloseTask(new ChannelListener<WebSocketChannel>() { @Override public void handleEvent(WebSocketChannel channel) { channel.getReceiveSetter().set(new AbstractReceiveListener() { protected void onError (WebSocketChannel channel, Throwable error) { endpoint.onError(channel, error); channel.resumeReceives(); } else { try { channel.close(); } catch (IOException e) { throw new RuntimeException("Error closing websocket", e);
@Override public void onConnect(WebSocketHttpExchange exchange, WebSocketChannel channel) { channel.getReceiveSetter().set(this); channel.resumeReceives(); connection = new UndertowWebSocketConnection(exchange, channel); connections.add(connection); channel.addCloseTask(ch -> connections.remove(connection)); context = new WebSocketContext(connectionsReadOnly, connection, pathParameters); handler.onOpen(context); }
@Override public void onConnect(WebSocketHttpExchange exchange, WebSocketChannel channel) { LOG.trace("onConnect {}", exchange); final String connectionKey = UUID.randomUUID().toString(); channel.setAttribute(UndertowConstants.CONNECTION_KEY, connectionKey); channel.getReceiveSetter().set(receiveListener); channel.addCloseTask(closeListener); sendEventNotificationIfNeeded(connectionKey, EventType.ONOPEN); channel.resumeReceives(); }
UndertowEndpoint createEndpoint(WebSocketChannel channel) { final UndertowEndpoint endpoint = new UndertowEndpoint(this, channel); try { channel.setOption(Options.TCP_NODELAY, NODELAY); } catch (IOException e) { throw new OptionAssignmentException("Error setting option", e); } channel.getReceiveSetter().set(endpoint); channel.resumeReceives(); scanner.addEndpoint(endpoint); listener.onConnect(endpoint); if (idleTimeoutMillis != 0) { channel.setIdleTimeout(idleTimeoutMillis); } return endpoint; }
private void registerWebSocketChannelListener(WebSocketChannel webSocketChannel) { ChannelListener<WebSocketChannel> listener = new AbstractReceiveListener() { @Override protected void onFullBinaryMessage(WebSocketChannel channel, BufferedBinaryMessage message) throws IOException { log.trace("Server received full binary message"); Pooled<ByteBuffer[]> pulledData = message.getData(); try { ByteBuffer[] resource = pulledData.getResource(); ByteBuffer byteBuffer = WebSockets.mergeBuffers(resource); String msg = new String(byteBuffer.array()); log.trace("Sending message to decoder: {}", msg); writeToDecoder(msg); } finally { pulledData.discard(); } } }; webSocketChannel.getReceiveSetter().set(listener); }
public void deregister() { synchronized (LOCK) { try { if (webSocketChannel != null && webSocketChannel.isOpen()) { logger.info("SSR: Closing WS webSocketChannel"); webSocketChannel.setCloseCode(1000); //normal closure webSocketChannel.setCloseReason("Service disconnected"); webSocketChannel.sendClose(); } } catch (IOException e) { logger.error("SSR: Error while closing the webSocketChannel", e); } } }
@Override public void close() throws IOException { if ( channel != null ) { ChannelListener.Setter<WebSocketChannel> receiveSetter = channel.getReceiveSetter(); if ( receiveSetter != null ) receiveSetter.set(null); channel.close(); } if ( sink != null ) { ObjectSink objectSink = sink.get(); if (objectSink != null) objectSink.sinkClosed(); } conf = null; channel = null; ex = null; }
@Override public void close() throws IOException { if (channel.isOpen() && ! channel.isCloseFrameSent()) { channel.sendClose(); } else { terminate(); } }
/** * Get the request URI scheme. Normally this is one of {@code ws} or {@code wss}. * * @return the request URI scheme */ public String getRequestScheme() { if (getUrl().startsWith("wss:")) { return "wss"; } else { return "ws"; } }
@Override public void closeConnection() { if (WebsocketRequestBuilderDefault.this.connectionIsClosed) { WebsocketRequestBuilderDefault.this.logger.info("Connection is already closed..."); return; } WebsocketRequestBuilderDefault.this.connectionIsClosed = true; try { if (channel.isOpen()) { channel.close(); } } catch (Exception ex) { WebsocketRequestBuilderDefault.this.logger.error("Erreur closing the connection: " + ex.getMessage()); } }
@Override protected void lastDataRead() { if(!closeFrameReceived && !closeFrameSent) { //the peer has likely already gone away, but try and send a close frame anyway //this will likely just result in the write() failing an immediate connection termination //which is what we want closeFrameReceived = true; //not strictly true, but the read side is gone try { sendClose(); } catch (IOException e) { IoUtils.safeClose(this); } } }
@Override public Mono<Void> close(CloseStatus status) { CloseMessage cm = new CloseMessage(status.getCode(), status.getReason()); if (!getDelegate().isCloseFrameSent()) { WebSockets.sendClose(cm, getDelegate(), null); } return Mono.empty(); }
List<WebSocketChannel> retrievePeerConnectionsForCurrentURLRequest( final WebSocketChannel channel ) { final List<WebSocketChannel> hashSet = new ArrayList<>(); for ( final WebSocketChannel peer : channel.getPeerConnections() ) if ( channel.getUrl().equals( peer.getUrl() ) ) hashSet.add( peer ); return hashSet; }
public void connect(final WebSocketChannel channel) { this.channel = channel; this.channel.setIdleTimeout(idleTimeout); this.channel.getReceiveSetter().set(this); this.channel.resumeReceives(); this.onConnectCallback.run(); ready.countDown(); }