protected StandardWebSocketSession createWebSocketSession(Session session, HandshakeInfo info, MonoProcessor<Void> completion) { return new StandardWebSocketSession(session, info, this.bufferFactory, completion); }
@Override public void onResult(SendResult result) { if (result.isOK()) { getSendProcessor().setReadyToSend(true); getSendProcessor().onWritePossible(); } else { getSendProcessor().cancel(); getSendProcessor().onError(result.getException()); } }
@Override public void onClose(Session session, CloseReason reason) { if (this.delegateSession != null) { int code = reason.getCloseCode().getCode(); this.delegateSession.handleClose(new CloseStatus(code, reason.getReasonPhrase())); } }
@Override protected boolean sendMessage(WebSocketMessage message) throws IOException { ByteBuffer buffer = message.getPayload().asByteBuffer(); if (WebSocketMessage.Type.TEXT.equals(message.getType())) { getSendProcessor().setReadyToSend(false); String text = new String(buffer.array(), StandardCharsets.UTF_8); getDelegate().getAsyncRemote().sendText(text, new SendProcessorCallback()); } else if (WebSocketMessage.Type.BINARY.equals(message.getType())) { getSendProcessor().setReadyToSend(false); getDelegate().getAsyncRemote().sendBinary(buffer, new SendProcessorCallback()); } else if (WebSocketMessage.Type.PING.equals(message.getType())) { getDelegate().getAsyncRemote().sendPing(buffer); } else if (WebSocketMessage.Type.PONG.equals(message.getType())) { getDelegate().getAsyncRemote().sendPong(buffer); } else { throw new IllegalArgumentException("Unexpected message type: " + message.getType()); } return true; }
@Override public Mono<Void> close(CloseStatus status) { try { CloseReason.CloseCode code = CloseCodes.getCloseCode(status.getCode()); getDelegate().close(new CloseReason(code, status.getReason())); } catch (IOException ex) { return Mono.error(ex); } return Mono.empty(); }
@Override public void onError(Session session, Throwable exception) { if (this.delegateSession != null) { this.delegateSession.handleError(exception); } }
@Override public void onOpen(Session session, EndpointConfig config) { this.delegateSession = this.sessionFactory.apply(session); Assert.state(this.delegateSession != null, "No delegate session"); session.addMessageHandler(String.class, message -> { WebSocketMessage webSocketMessage = toMessage(message); this.delegateSession.handleMessage(webSocketMessage.getType(), webSocketMessage); }); session.addMessageHandler(ByteBuffer.class, message -> { WebSocketMessage webSocketMessage = toMessage(message); this.delegateSession.handleMessage(webSocketMessage.getType(), webSocketMessage); }); session.addMessageHandler(PongMessage.class, message -> { WebSocketMessage webSocketMessage = toMessage(message); this.delegateSession.handleMessage(webSocketMessage.getType(), webSocketMessage); }); this.delegateHandler.handle(this.delegateSession).subscribe(this.delegateSession); }
@Override protected boolean sendMessage(WebSocketMessage message) throws IOException { ByteBuffer buffer = message.getPayload().asByteBuffer(); if (WebSocketMessage.Type.TEXT.equals(message.getType())) { getSendProcessor().setReadyToSend(false); String text = new String(buffer.array(), StandardCharsets.UTF_8); getDelegate().getAsyncRemote().sendText(text, new SendProcessorCallback()); } else if (WebSocketMessage.Type.BINARY.equals(message.getType())) { getSendProcessor().setReadyToSend(false); getDelegate().getAsyncRemote().sendBinary(buffer, new SendProcessorCallback()); } else if (WebSocketMessage.Type.PING.equals(message.getType())) { getDelegate().getAsyncRemote().sendPing(buffer); } else if (WebSocketMessage.Type.PONG.equals(message.getType())) { getDelegate().getAsyncRemote().sendPong(buffer); } else { throw new IllegalArgumentException("Unexpected message type: " + message.getType()); } return true; }
@Override public Mono<Void> close(CloseStatus status) { try { CloseReason.CloseCode code = CloseCodes.getCloseCode(status.getCode()); getDelegate().close(new CloseReason(code, status.getReason())); } catch (IOException e) { return Mono.error(e); } return Mono.empty(); }
@Override public void onError(Session session, Throwable exception) { if (this.delegateSession != null) { this.delegateSession.handleError(exception); } }
@Override public void onOpen(Session session, EndpointConfig config) { this.delegateSession = this.sessionFactory.apply(session); session.addMessageHandler(String.class, message -> { WebSocketMessage webSocketMessage = toMessage(message); this.delegateSession.handleMessage(webSocketMessage.getType(), webSocketMessage); }); session.addMessageHandler(ByteBuffer.class, message -> { WebSocketMessage webSocketMessage = toMessage(message); this.delegateSession.handleMessage(webSocketMessage.getType(), webSocketMessage); }); session.addMessageHandler(PongMessage.class, message -> { WebSocketMessage webSocketMessage = toMessage(message); this.delegateSession.handleMessage(webSocketMessage.getType(), webSocketMessage); }); this.delegateHandler.handle(this.delegateSession).subscribe(this.delegateSession); }
private StandardWebSocketHandlerAdapter createEndpoint(URI url, WebSocketHandler handler, MonoProcessor<Void> completion, DefaultConfigurator configurator) { return new StandardWebSocketHandlerAdapter(handler, session -> { HttpHeaders responseHeaders = configurator.getResponseHeaders(); HandshakeInfo info = afterHandshake(url, responseHeaders); return new StandardWebSocketSession(session, info, this.bufferFactory, completion); }); }
@Override public void onClose(Session session, CloseReason reason) { if (this.delegateSession != null) { int code = reason.getCloseCode().getCode(); this.delegateSession.handleClose(new CloseStatus(code, reason.getReasonPhrase())); } }
@Override public void onResult(SendResult result) { if (result.isOK()) { getSendProcessor().setReadyToSend(true); getSendProcessor().onWritePossible(); } else { getSendProcessor().cancel(); getSendProcessor().onError(result.getException()); } }
@Override public Mono<Void> upgrade(ServerWebExchange exchange, WebSocketHandler handler, Optional<String> subProtocol){ ServerHttpRequest request = exchange.getRequest(); ServerHttpResponse response = exchange.getResponse(); HttpServletRequest servletRequest = getHttpServletRequest(request); HttpServletResponse servletResponse = getHttpServletResponse(response); Endpoint endpoint = new StandardWebSocketHandlerAdapter(handler, session -> { HandshakeInfo info = getHandshakeInfo(exchange, subProtocol); DataBufferFactory factory = response.bufferFactory(); return new StandardWebSocketSession(session, info, factory); }); String requestURI = servletRequest.getRequestURI(); DefaultServerEndpointConfig config = new DefaultServerEndpointConfig(requestURI, endpoint); config.setSubprotocols(subProtocol.map(Collections::singletonList).orElse(Collections.emptyList())); try { WsServerContainer container = getContainer(servletRequest); container.doUpgrade(servletRequest, servletResponse, config, Collections.emptyMap()); } catch (ServletException | IOException ex) { return Mono.error(ex); } return Mono.empty(); }