use of org.springframework.web.reactive.socket.HandshakeInfo in project spring-framework by spring-projects.
the class UndertowWebSocketClient method handleChannel.
private void handleChannel(URI url, WebSocketHandler handler, MonoProcessor<Void> completion, DefaultNegotiation negotiation, WebSocketChannel channel) {
HandshakeInfo info = afterHandshake(url, negotiation.getResponseHeaders());
UndertowWebSocketSession session = new UndertowWebSocketSession(channel, info, bufferFactory, completion);
UndertowWebSocketHandlerAdapter adapter = new UndertowWebSocketHandlerAdapter(session);
channel.getReceiveSetter().set(adapter);
channel.resumeReceives();
handler.handle(session).subscribe(session);
}
use of org.springframework.web.reactive.socket.HandshakeInfo 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.HandshakeInfo in project spring-framework by spring-projects.
the class HandshakeWebSocketServiceTests method sessionAttributePredicate.
@Test
public void sessionAttributePredicate() {
MockWebSession session = new MockWebSession();
session.getAttributes().put("a1", "v1");
session.getAttributes().put("a2", "v2");
session.getAttributes().put("a3", "v3");
session.getAttributes().put("a4", "v4");
session.getAttributes().put("a5", "v5");
MockServerHttpRequest request = initHandshakeRequest();
MockServerWebExchange exchange = MockServerWebExchange.builder(request).session(session).build();
TestRequestUpgradeStrategy upgradeStrategy = new TestRequestUpgradeStrategy();
HandshakeWebSocketService service = new HandshakeWebSocketService(upgradeStrategy);
service.setSessionAttributePredicate(name -> Arrays.asList("a1", "a3", "a5").contains(name));
service.handleRequest(exchange, mock(WebSocketHandler.class)).block();
HandshakeInfo info = upgradeStrategy.handshakeInfo;
assertThat(info).isNotNull();
Map<String, Object> attributes = info.getAttributes();
assertThat(attributes).hasSize(3).containsEntry("a1", "v1").containsEntry("a3", "v3").containsEntry("a5", "v5");
}
use of org.springframework.web.reactive.socket.HandshakeInfo in project spring-framework by spring-projects.
the class ReactorNettyRequestUpgradeStrategy method upgrade.
@Override
public Mono<Void> upgrade(ServerWebExchange exchange, WebSocketHandler handler, @Nullable String subProtocol, Supplier<HandshakeInfo> handshakeInfoFactory) {
ServerHttpResponse response = exchange.getResponse();
HttpServerResponse reactorResponse = ServerHttpResponseDecorator.getNativeResponse(response);
HandshakeInfo handshakeInfo = handshakeInfoFactory.get();
NettyDataBufferFactory bufferFactory = (NettyDataBufferFactory) response.bufferFactory();
URI uri = exchange.getRequest().getURI();
// Trigger WebFlux preCommit actions and upgrade
return response.setComplete().then(Mono.defer(() -> {
WebsocketServerSpec spec = buildSpec(subProtocol);
return reactorResponse.sendWebsocket((in, out) -> {
ReactorNettyWebSocketSession session = new ReactorNettyWebSocketSession(in, out, handshakeInfo, bufferFactory, spec.maxFramePayloadLength());
return handler.handle(session).checkpoint(uri + " [ReactorNettyRequestUpgradeStrategy]");
}, spec);
}));
}
use of org.springframework.web.reactive.socket.HandshakeInfo in project spring-framework by spring-projects.
the class UndertowRequestUpgradeStrategy method upgrade.
@Override
public Mono<Void> upgrade(ServerWebExchange exchange, WebSocketHandler handler, @Nullable String subProtocol, Supplier<HandshakeInfo> handshakeInfoFactory) {
HttpServerExchange httpExchange = ServerHttpRequestDecorator.getNativeRequest(exchange.getRequest());
Set<String> protocols = (subProtocol != null ? Collections.singleton(subProtocol) : Collections.emptySet());
Hybi13Handshake handshake = new Hybi13Handshake(protocols, false);
List<Handshake> handshakes = Collections.singletonList(handshake);
HandshakeInfo handshakeInfo = handshakeInfoFactory.get();
DataBufferFactory bufferFactory = exchange.getResponse().bufferFactory();
// Trigger WebFlux preCommit actions and upgrade
return exchange.getResponse().setComplete().then(Mono.deferContextual(contextView -> {
DefaultCallback callback = new DefaultCallback(handshakeInfo, ContextWebSocketHandler.decorate(handler, contextView), bufferFactory);
try {
new WebSocketProtocolHandshakeHandler(handshakes, callback).handleRequest(httpExchange);
} catch (Exception ex) {
return Mono.error(ex);
}
return Mono.empty();
}));
}
Aggregations