use of org.springframework.web.reactive.socket.WebSocketSession 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.WebSocketSession 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.WebSocketSession in project spring-framework by spring-projects.
the class ReactorNettyWebSocketClient method execute.
@Override
public Mono<Void> execute(URI url, HttpHeaders headers, WebSocketHandler handler) {
String[] protocols = beforeHandshake(url, headers, handler);
return getHttpClient().ws(url.toString(), nettyHeaders -> setNettyHeaders(headers, nettyHeaders), StringUtils.arrayToCommaDelimitedString(protocols)).then(response -> {
HandshakeInfo info = afterHandshake(url, toHttpHeaders(response));
ByteBufAllocator allocator = response.channel().alloc();
NettyDataBufferFactory factory = new NettyDataBufferFactory(allocator);
return response.receiveWebsocket((in, out) -> {
WebSocketSession session = new ReactorNettyWebSocketSession(in, out, info, factory);
return handler.handle(session);
});
});
}
Aggregations