Skip to content

Commit

Permalink
add SSEController
Browse files Browse the repository at this point in the history
  • Loading branch information
cmonkey committed Mar 6, 2018
1 parent bfd50db commit e905b9e
Showing 1 changed file with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.github.cmonkey.webflux.controller;

import org.springframework.data.redis.connection.RedisZSetCommands;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.util.function.Tuples;

import java.time.Duration;
import java.util.concurrent.ThreadLocalRandom;

@RestController
@RequestMapping("/sse")
public class SSEController {

@GetMapping("/randomNumbers")
public Flux<ServerSentEvent<Integer>> randomNumbers(){

return Flux.interval(Duration.ofSeconds(1))
.map(seq -> Tuples.of(seq, ThreadLocalRandom.current().nextInt()))
.map(data -> ServerSentEvent.<Integer>builder()
.event("random")
.id(Long.toString(data.getT1()))
.data(data.getT2())
.build());
}
}

0 comments on commit e905b9e

Please sign in to comment.