WebFlux系列(三) Server-Sent Events(续)

发布于 1月6日  约 4 分钟

编程#入门#java#spring#webflux#SSE#reactor

SSE数据结构

视频讲解: https://www.bilibili.com/vide...

WebfluxServerApplication.java

package com.example.webfluxserver;

import lombok.extern.log4j.Log4j2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.time.Duration;
import java.util.UUID;

@Log4j2
@SpringBootApplication
public class WebfluxServerApplication extends BaseApplication{
    public static void main(String[] args) { SpringApplication.run(WebfluxServerApplication.class, args); }
    @RestController
    class EmployeeController{

        @CrossOrigin
        @GetMapping(value = "sse")
        public Flux<ServerSentEvent<String>> sse(){
            return  Flux.interval(Duration.ofMillis(1000)).map(val ->{
                return ServerSentEvent.<String>builder()
                        .id(UUID.randomUUID().toString())
                        .event("ping")
                        .data(val.toString())
                        .build();
            });
        }
    }
}

公众号,坚持每天3分钟视频学习

阅读 177发布于 1月6日

推荐阅读
目录