Java Code Examples for org.springframework.web.reactive.socket.WebSocketSession#send()

The following examples show how to use org.springframework.web.reactive.socket.WebSocketSession#send() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EmployeeWebSocketHandler.java    From tutorials with MIT License 6 votes vote down vote up
@Override
public Mono<Void> handle(WebSocketSession webSocketSession) {

    Flux<String> employeeCreationEvent = Flux.generate(sink -> {
        EmployeeCreationEvent event = new EmployeeCreationEvent(randomUUID().toString(), now().toString());
        try {
            sink.next(om.writeValueAsString(event));
        } catch (JsonProcessingException e) {
            sink.error(e);
        }
    });

    return webSocketSession.send(employeeCreationEvent
        .map(webSocketSession::textMessage)
        .delayElements(Duration.ofSeconds(1)));
}
 
Example 2
Source File: WebSocketSampleApplication.java    From vertx-spring-boot with Apache License 2.0 5 votes vote down vote up
private Mono<Void> toUppercaseHandler(WebSocketSession session) {
    Flux<WebSocketMessage> messages = session.receive() // Get incoming messages stream
        .filter(message -> message.getType() == WebSocketMessage.Type.TEXT) // Filter out non-text messages
        .map(message -> message.getPayloadAsText().toUpperCase()) // Execute service logic
        .map(session::textMessage); // Create a response message

    return session.send(messages); // Send response messages
}
 
Example 3
Source File: EchoHandler.java    From soul with Apache License 2.0 5 votes vote down vote up
@Override
public Mono<Void> handle(final WebSocketSession session) {
    return session.send(
            session.receive()
                    .map(msg -> session.textMessage(
                            "result xiaoyu, -> " + msg.getPayloadAsText())));
}
 
Example 4
Source File: SampleWebSocketHandler.java    From Spring-5.0-Projects with MIT License 5 votes vote down vote up
@Override
public Mono<Void> handle(WebSocketSession webSocketSession) {
	Flux<Student> allStudentSource = studentMongoRepository.findAll();
	System.out.println(" ****** Incoming messages ****** ");
	webSocketSession.receive().subscribe(System.out::println);
	
	System.out.println(" ****** Sending Student data ****** ");
	return webSocketSession.send(allStudentSource.map(student->{
		return writeValueAsSTring(student);
	 }).map(webSocketSession::textMessage)
   );
}
 
Example 5
Source File: TimeHandler.java    From reactor-workshop with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Mono<Void> handle(WebSocketSession session) {
    final Flux<WebSocketMessage> outMessages = Flux
            .interval(Duration.ofMillis(500))
            .timestamp()
            .map(t -> t.getT2() + "\t" + Instant.ofEpochMilli(t.getT1()))
            .map(session::textMessage)
            .doOnSubscribe(s -> log.info("Got new connection {}", session))
            .doOnComplete(() -> log.info("Connection completed {}", session));
    return session.send(outMessages);
}
 
Example 6
Source File: EchoHandler.java    From springboot-learning-example with Apache License 2.0 5 votes vote down vote up
@Override
public Mono<Void> handle(final WebSocketSession session) {
    return session.send(
            session.receive()
                    .map(msg -> session.textMessage(
                            "服务端返回:小明, " + msg.getPayloadAsText())));
}
 
Example 7
Source File: WebSocketIntegrationTests.java    From spring-cloud-gateway with Apache License 2.0 5 votes vote down vote up
private static Mono<Void> doSend(WebSocketSession session,
		Publisher<WebSocketMessage> output) {
	return session.send(output);
	// workaround for suspected RxNetty WebSocket client issue
	// https://github.com/ReactiveX/RxNetty/issues/560
	// return session.send(Mono.delay(Duration.ofMillis(100)).thenMany(output));
}
 
Example 8
Source File: WebSocketIT.java    From vertx-spring-boot with Apache License 2.0 4 votes vote down vote up
private Mono<Void> echoHandler(WebSocketSession session) {
    return session.send(session.receive());
}
 
Example 9
Source File: WebSocketIT.java    From vertx-spring-boot with Apache License 2.0 4 votes vote down vote up
private Mono<Void> doubleProducerHandler(WebSocketSession session) {
    Mono<Void> firstSend = session.send(Mono.just(session.textMessage("ping")));
    Mono<Void> secondSend = session.send(Mono.just(session.textMessage("pong")));

    return firstSend.then(secondSend);
}
 
Example 10
Source File: DefaultWebSocketHandler.java    From webFluxTemplate with MIT License 4 votes vote down vote up
public Mono<Void> doHandle(WebSocketSession session) {
    // Use retain() for Reactor Netty
    return session.send(session.receive().doOnNext(WebSocketMessage::retain).delayElements(Duration.ofSeconds(2)));
}
 
Example 11
Source File: EchoWebSocketHandler.java    From spring-reactive-sample with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Mono<Void> handle(WebSocketSession session) {
    // Use retain() for Reactor Netty
    return session.send(session.receive().doOnNext(WebSocketMessage::retain));
}
 
Example 12
Source File: PostsWebSocketHandler.java    From spring-reactive-sample with GNU General Public License v3.0 4 votes vote down vote up
private Mono<Void> doSend(WebSocketSession session, Publisher<WebSocketMessage> output) {
    return session.send(Mono.delay(Duration.ofMillis(100)).thenMany(output));
}
 
Example 13
Source File: EchoHandler.java    From tools-journey with Apache License 2.0 4 votes vote down vote up
@Override
public Mono<Void> handle(final WebSocketSession session) {
    return session.send(
            session.receive()
                    .map(msg -> session.textMessage("ECHO -> " + msg.getPayloadAsText())));
}
 
Example 14
Source File: WebSocketIntegrationTests.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
@Override
public Mono<Void> handle(WebSocketSession session) {
	// Use retain() for Reactor Netty
	return session.send(session.receive().doOnNext(WebSocketMessage::retain));
}
 
Example 15
Source File: EchoWebSocketHandler.java    From spring-reactive-playground with Apache License 2.0 4 votes vote down vote up
@Override
public Mono<Void> handle(WebSocketSession session) {
	// Use retain() for Reactor Netty
	return session.send(session.receive().doOnNext(WebSocketMessage::retain).delay(Duration.ofSeconds(2)));
}