Udated stream api

This commit is contained in:
Sambo Chea 2022-03-25 16:01:50 +07:00
parent 4c8e4c31fb
commit 7a87fdd2cc
Signed by: sombochea
GPG Key ID: 3C7CF22A05D95490

View File

@ -5,15 +5,15 @@ import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication import org.springframework.boot.runApplication
import org.springframework.context.ApplicationEventPublisher import org.springframework.context.ApplicationEventPublisher
import org.springframework.web.bind.annotation.GetMapping import org.springframework.scheduling.annotation.Async
import org.springframework.web.bind.annotation.RequestMapping import org.springframework.scheduling.annotation.EnableAsync
import org.springframework.web.bind.annotation.RequestParam import org.springframework.web.bind.annotation.*
import org.springframework.web.bind.annotation.RestController
import reactor.core.publisher.Flux import reactor.core.publisher.Flux
import reactor.core.publisher.Flux.generate import reactor.core.publisher.Flux.generate
import java.util.concurrent.ThreadLocalRandom import java.util.concurrent.ThreadLocalRandom
@SpringBootApplication @SpringBootApplication
@EnableAsync
class LiveServiceApplication class LiveServiceApplication
fun main(args: Array<String>) { fun main(args: Array<String>) {
@ -33,6 +33,7 @@ class HelloController(
events = Flux.create(eventPublisher).share() events = Flux.create(eventPublisher).share()
} }
@Async
@RequestMapping("/publish") @RequestMapping("/publish")
fun publish(@RequestParam(value = "message", defaultValue = "Hello World") message: String): Map<String, Any> { fun publish(@RequestParam(value = "message", defaultValue = "Hello World") message: String): Map<String, Any> {
applicationEventPublisher.publishEvent(MessageCreatedEvent(message)) applicationEventPublisher.publishEvent(MessageCreatedEvent(message))
@ -43,6 +44,7 @@ class HelloController(
} }
@GetMapping("/stream/chat", produces = ["text/event-stream"]) @GetMapping("/stream/chat", produces = ["text/event-stream"])
@CrossOrigin(value = ["*"])
fun streamChat(): Flux<String> { fun streamChat(): Flux<String> {
return this.events.map { event -> return this.events.map { event ->
try { try {