Created
February 1, 2018 16:11
-
-
Save pdemanget/b6bbef7d5239089cedbe2de40e2f4863 to your computer and use it in GitHub Desktop.
Server Sent event
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Server sent event dom using Spring webflux. | |
* | |
* @author Philippe.DEMANGET | |
*/ | |
@RestController | |
@RequestMapping("/event/stream") | |
public class EventStreamController { | |
private Logger logger = LoggerFactory.getLogger(getClass()); | |
private List<FluxSink<Line>> fluxSinks = new ArrayList<>(); | |
/** | |
* Simule une alarme toutes les 10 s | |
*/ | |
@Scheduled(fixedRate = 1_000) | |
public void sendAlarm() { | |
logger.trace("SEND2"); | |
System.out.println("SEND2"); | |
fluxSinks.forEach(fluxSink -> { | |
fluxSink.next(new Line()); | |
}); | |
} | |
/** | |
* Flux d'évènements en SSE. | |
* @return | |
*/ | |
//@GetMapping( produces = MediaType.APPLICATION_STREAM_JSON_VALUE) | |
@GetMapping( produces = MediaType.TEXT_EVENT_STREAM_VALUE) | |
public Flux<Line> stockTransactionEvents() { | |
return Flux.create(fluxSink -> { | |
fluxSinks.add(fluxSink); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment