use of org.springframework.web.reactive.socket.WebSocketMessage in project spring-framework by spring-projects.
the class JettyWebSocketHandlerAdapter method onWebSocketBinary.
@OnWebSocketMessage
public void onWebSocketBinary(byte[] message, int offset, int length) {
if (this.delegateSession != null) {
ByteBuffer buffer = ByteBuffer.wrap(message, offset, length);
WebSocketMessage webSocketMessage = toMessage(Type.BINARY, buffer);
this.delegateSession.handleMessage(webSocketMessage.getType(), webSocketMessage);
}
}
use of org.springframework.web.reactive.socket.WebSocketMessage in project spring-framework by spring-projects.
the class JettyWebSocketHandlerAdapter method toMessage.
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);
}
}
use of org.springframework.web.reactive.socket.WebSocketMessage in project spring-framework by spring-projects.
the class StandardWebSocketHandlerAdapter method onOpen.
@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);
}
use of org.springframework.web.reactive.socket.WebSocketMessage in project spring-cloud-gateway by spring-cloud.
the class WebSocketIntegrationTests method customHeader.
@Test
public void customHeader() throws Exception {
HttpHeaders headers = new HttpHeaders();
headers.add("my-header", "my-value");
MonoProcessor<Object> output = MonoProcessor.create();
client.execute(getUrl("/custom-header"), headers, session -> session.receive().map(WebSocketMessage::getPayloadAsText).subscribeWith(output).then()).block(Duration.ofMillis(5000));
assertEquals("my-header:my-value", output.block(Duration.ofMillis(5000)));
}
use of org.springframework.web.reactive.socket.WebSocketMessage in project spring-framework by spring-projects.
the class JettyWebSocketHandlerAdapter method onWebSocketFrame.
@OnWebSocketFrame
public void onWebSocketFrame(Frame frame) {
if (this.delegateSession != null) {
if (OpCode.PONG == frame.getOpCode()) {
ByteBuffer buffer = (frame.getPayload() != null ? frame.getPayload() : EMPTY_PAYLOAD);
WebSocketMessage webSocketMessage = toMessage(Type.PONG, buffer);
this.delegateSession.handleMessage(webSocketMessage.getType(), webSocketMessage);
}
}
}
Aggregations