Refactor reactive WebSocketClient contract

Switch from returning Mono<WebSocketSession> to take a WebSocketHandler
and return Mono<Void> for the entire session handling.

The WebSocketHandler callback delimits the stard and end of protocol
handling and forces the handler to operate within the scope of the
Reactor operators.

Give the full duplex nature of WebSockets, the symmetry between client
and server (each now using WebSocketHandler) also seems appropriate.

Issue: SPR-14527
This commit is contained in:
Rossen Stoyanchev
2016-12-19 11:57:43 -05:00
parent 9c55d22f78
commit 14068d5274
4 changed files with 45 additions and 27 deletions

View File

@@ -16,8 +16,11 @@
package org.springframework.web.reactive.socket.server;
import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.tomcat.websocket.server.WsContextListener;
import org.jetbrains.annotations.NotNull;
import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
@@ -112,6 +115,12 @@ public abstract class AbstractWebSocketIntegrationTests {
}
}
@NotNull
protected URI getUrl(String path) throws URISyntaxException {
return new URI("ws://localhost:" + this.port + path);
}
static abstract class AbstractHandlerAdapterConfig {
@Bean

View File

@@ -15,13 +15,13 @@
*/
package org.springframework.web.reactive.socket.server;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.core.publisher.ReplayProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -35,7 +35,6 @@ import static org.junit.Assert.assertEquals;
/**
* Integration tests with server-side {@link WebSocketHandler}s.
*
* @author Rossen Stoyanchev
*/
@SuppressWarnings({"unused", "WeakerAccess"})
@@ -52,9 +51,10 @@ public class WebSocketIntegrationTests extends AbstractWebSocketIntegrationTests
public void echo() throws Exception {
int count = 100;
Flux<String> input = Flux.range(1, count).map(index -> "msg-" + index);
Flux<String> output = new RxNettyWebSocketClient()
.connect(new URI("ws://localhost:" + this.port + "/echo"))
.flatMap(session -> session
ReplayProcessor<Object> emitter = ReplayProcessor.create(count);
new RxNettyWebSocketClient()
.execute(getUrl("/echo"), session -> session
.send(input.map(session::textMessage))
.thenMany(session.receive()
.take(count)
@@ -62,9 +62,12 @@ public class WebSocketIntegrationTests extends AbstractWebSocketIntegrationTests
String text = message.getPayloadAsText();
message.release();
return text;
})
));
assertEquals(input.collectList().blockMillis(5000), output.collectList().blockMillis(5000));
}))
.subscribeWith(emitter)
.then())
.blockMillis(5000);
assertEquals(input.collectList().blockMillis(5000), emitter.collectList().blockMillis(5000));
}