use of org.springframework.web.reactive.socket.WebSocketMessage in project spring-framework by spring-projects.
the class StandardWebSocketHandlerAdapter method toMessage.
private <T> WebSocketMessage toMessage(T message) {
WebSocketSession session = this.delegateSession;
Assert.state(session != null, "Cannot create message without a session");
if (message instanceof String) {
byte[] bytes = ((String) message).getBytes(StandardCharsets.UTF_8);
return new WebSocketMessage(Type.TEXT, session.bufferFactory().wrap(bytes));
} else if (message instanceof ByteBuffer) {
DataBuffer buffer = session.bufferFactory().wrap((ByteBuffer) message);
return new WebSocketMessage(Type.BINARY, buffer);
} else if (message instanceof PongMessage) {
DataBuffer buffer = session.bufferFactory().wrap(((PongMessage) message).getApplicationData());
return new WebSocketMessage(Type.PONG, buffer);
} else {
throw new IllegalArgumentException("Unexpected message type: " + message);
}
}
use of org.springframework.web.reactive.socket.WebSocketMessage in project tutorials by eugenp.
the class ReactiveJavaClientWebSocket method main.
public static void main(String[] args) throws InterruptedException {
WebSocketClient client = new ReactorNettyWebSocketClient();
client.execute(URI.create("ws://localhost:8080/event-emitter"), session -> session.send(Mono.just(session.textMessage("event-spring-reactive-client-websocket"))).thenMany(session.receive().map(WebSocketMessage::getPayloadAsText).log()).then()).block(Duration.ofSeconds(10L));
}
use of org.springframework.web.reactive.socket.WebSocketMessage in project spring-cloud-gateway by spring-cloud.
the class WebSocketIntegrationTests method subProtocol.
@Test
public void subProtocol() throws Exception {
String protocol = "echo-v1";
String protocol2 = "echo-v2";
AtomicReference<HandshakeInfo> infoRef = new AtomicReference<>();
MonoProcessor<Object> output = MonoProcessor.create();
client.execute(getUrl("/sub-protocol"), new WebSocketHandler() {
@Override
public List<String> getSubProtocols() {
return Arrays.asList(protocol, protocol2);
}
@Override
public Mono<Void> handle(WebSocketSession session) {
infoRef.set(session.getHandshakeInfo());
return session.receive().map(WebSocketMessage::getPayloadAsText).subscribeWith(output).then();
}
}).block(Duration.ofMillis(5000));
HandshakeInfo info = infoRef.get();
assertThat(info.getHeaders().getFirst("Upgrade")).isEqualToIgnoringCase("websocket");
assertThat(info.getHeaders().getFirst("Sec-WebSocket-Protocol")).isEqualTo(protocol);
assertThat(info.getSubProtocol()).as("Wrong protocol accepted").isEqualTo(protocol);
assertThat(output.block(Duration.ofSeconds(5))).as("Wrong protocol detected on the server side").isEqualTo(protocol);
}
use of org.springframework.web.reactive.socket.WebSocketMessage in project spring-framework by spring-projects.
the class AbstractWebSocketSession method textMessage.
// WebSocketMessage factory methods
@Override
public WebSocketMessage textMessage(String payload) {
byte[] bytes = payload.getBytes(StandardCharsets.UTF_8);
DataBuffer buffer = bufferFactory().wrap(bytes);
return new WebSocketMessage(WebSocketMessage.Type.TEXT, buffer);
}
use of org.springframework.web.reactive.socket.WebSocketMessage in project spring-framework by spring-projects.
the class JettyWebSocketHandlerAdapter method onWebSocketText.
@OnWebSocketMessage
public void onWebSocketText(String message) {
if (this.delegateSession != null) {
WebSocketMessage webSocketMessage = toMessage(Type.TEXT, message);
this.delegateSession.handleMessage(webSocketMessage.getType(), webSocketMessage);
}
}
Aggregations