/** * Close the WebSocket session with the given status. * @param status the close status */ Mono<Void> close(CloseStatus status);
/** * A variant of {@link #getPayloadAsText(Charset)} that uses {@code UTF-8} * for decoding the raw content to text. */ public String getPayloadAsText() { return getPayloadAsText(StandardCharsets.UTF_8); }
private static String initLogPrefix(HandshakeInfo info, String id) { return info.getLogPrefix() != null ? info.getLogPrefix() : "[" + id + "] "; }
@Override public Mono<Void> handle(WebSocketSession session) { String protocol = session.getHandshakeInfo().getSubProtocol(); WebSocketMessage message = session.textMessage(protocol != null ? protocol : "none"); return session.send(Mono.just(message)); } }
@Override public Mono<Void> handle(WebSocketSession session) { HttpHeaders headers = session.getHandshakeInfo().getHeaders(); String payload = "my-header:" + headers.getFirst("my-header"); WebSocketMessage message = session.textMessage(payload); return session.send(Mono.just(message)); } }
@Bean public WebSocketService webSocketService() { return new HandshakeWebSocketService(getUpgradeStrategy()); }
private <T> WebSocketMessage toMessage(Type type, T message) { WebSocketSession session = this.delegateSession; Assert.state(session != null, "Cannot create message without a session"); if (Type.TEXT.equals(type)) { byte[] bytes = ((String) message).getBytes(StandardCharsets.UTF_8); DataBuffer buffer = session.bufferFactory().wrap(bytes); return new WebSocketMessage(Type.TEXT, buffer); } else if (Type.BINARY.equals(type)) { DataBuffer buffer = session.bufferFactory().wrap((ByteBuffer) message); return new WebSocketMessage(Type.BINARY, buffer); } else if (Type.PONG.equals(type)) { DataBuffer buffer = session.bufferFactory().wrap((ByteBuffer) message); return new WebSocketMessage(Type.PONG, buffer); } else { throw new IllegalArgumentException("Unexpected message type: " + message); } }
@Override public WebSocketMessage binaryMessage(Function<DataBufferFactory, DataBuffer> payloadFactory) { DataBuffer payload = payloadFactory.apply(bufferFactory()); return new WebSocketMessage(WebSocketMessage.Type.BINARY, payload); }
@Bean public WebSocketHandlerAdapter handlerAdapter() { return new WebSocketHandlerAdapter(webSocketService()); }
@OnWebSocketClose public void onWebSocketClose(int statusCode, String reason) { if (this.delegateSession != null) { this.delegateSession.handleClose(new CloseStatus(statusCode, reason)); } }
@Bean public HandlerMapping handlerMapping() { Map<String, WebSocketHandler> map = new HashMap<>(); map.put("/echo", new EchoWebSocketHandler()); map.put("/sub-protocol", new SubProtocolWebSocketHandler()); map.put("/custom-header", new CustomHeaderHandler()); map.put("/close", new SessionClosingHandler()); SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping(); mapping.setUrlMap(map); return mapping; }
/** * Create a new {@link CloseStatus} from this one with the specified reason. * @param reason the reason * @return a new {@link CloseStatus} instance */ public CloseStatus withReason(String reason) { Assert.hasText(reason, "Reason must not be empty"); return new CloseStatus(this.code, reason); }
@Override public boolean equals(Object other) { if (this == other) { return true; } if (!(other instanceof WebSocketMessage)) { return false; } WebSocketMessage otherMessage = (WebSocketMessage) other; return (this.type.equals(otherMessage.type) && ObjectUtils.nullSafeEquals(this.payload, otherMessage.payload)); }
@Override public int hashCode() { return this.type.hashCode() * 29 + this.payload.hashCode(); }
@Override public String toString() { return "WebSocket " + this.type.name() + " message (" + this.payload.readableByteCount() + " bytes)"; }
private HttpHandler createHttpHandler() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.register(DispatcherConfig.class, this.serverConfigClass); context.register(getWebConfigClass()); context.refresh(); return WebHttpHandlerBuilder.applicationContext(context).build(); }
@Override public WebSocketMessage pongMessage(Function<DataBufferFactory, DataBuffer> payloadFactory) { DataBuffer payload = payloadFactory.apply(bufferFactory()); return new WebSocketMessage(WebSocketMessage.Type.PONG, payload); }
@Override public WebSocketMessage pingMessage(Function<DataBufferFactory, DataBuffer> payloadFactory) { DataBuffer payload = payloadFactory.apply(bufferFactory()); return new WebSocketMessage(WebSocketMessage.Type.PING, payload); }