有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java服务器发送的事件不工作Spring Boot Webflux

我有一个简单的方法,它监听Redis Pub/Sub消息并发出服务器发送的事件,但由于某种原因,它不起作用。这就是我所拥有的

@GetMapping
fun getNotifications(@RequestParam(name = "token", required = true) token: String): Flux<ServerSentEvent<Notification>> {

    if (token.trim().isEmpty()) {
        return Flux.error(ResponseStatusException(HttpStatus.UNAUTHORIZED, "Invalid token"))
    }

    val claims = jwtService.getAllClaimsFromToken(token) ?: return Flux.error(ResponseStatusException(HttpStatus.UNAUTHORIZED, "Invalid token"))
    val userName = jwtService.getUsernameFromToken(claims) ?: return Flux.error(ResponseStatusException(HttpStatus.UNAUTHORIZED, "Invalid token"))

    return userRepository.findOneByEmail(userName)
                .filter { user -> checkAccess(user) }
                .thenMany<ServerSentEvent<Notification>> {
                    notificationService.subscribe()
                        .map { ServerSentEvent.builder<Notification>()
                                   .event("notification")
                                   .data(it)
                                   .build() 
                             }.asFlux()
                }.switchIfEmpty {
                    Flux.error<ResponseStatusException>(ResponseStatusException(HttpStatus.UNAUTHORIZED, "Token expired"))
                }
}

private fun checkAccess(user: User): Boolean {

    if (!user.enabled || !user.account.enabled) {
        return false
    }

    /// More checking... logic

    return true
}

我还缺什么吗


共 (0) 个答案