# Ktor 的实时、等候室与聊天

> 用 Kotlin 编写的 Ktor 3 插件：提供 pusher-js 和 Laravel Echo 需要的频道授权与 webhook 路由、等候室路由插件，并通过协程使用事件、Chat API 和 Approvals。

Source: https://hibira.io/zh-cn/guides/ktor

用 Kotlin 编写的 Ktor 3 插件：提供 pusher-js 和 Laravel Echo 需要的频道授权与 webhook 路由、等候室路由插件，并通过协程使用事件、Chat API 和 Approvals。

Package: io.hibira:hibira-ktor

- hibiraChannelAuth、hibiraUserAuth 和 hibiraWebhooks 添加 pusher-js 和 Laravel Echo 需要的路由。
- 在路由上 install(HibiraWaitingRoom)，没有 pass 的访客会被引导到托管的等候页。
- 事件、Chat API 和 Approvals 的 suspend 函数，以及 Live Chat 验证访客。

## 1. 安装

```kotlin
implementation("io.hibira:hibira-ktor:0.1.0")
```

## 2. 配置

```shell
HIBIRA_APP_ID=YOUR_APP_ID
HIBIRA_KEY=YOUR_APP_KEY
HIBIRA_SECRET=YOUR_APP_SECRET
HIBIRA_HOST=ws.hibira.io
```

## 3. 使用

```kotlin
fun Application.module() {
    install(HibiraPlugin)
    routing {
        hibiraChannelAuth { channel ->
            val user = principal<UserIdPrincipal>() ?: return@hibiraChannelAuth deny()
            if (channel.startsWith("presence-")) presence(user.name) else allow()
        }
        route("/drop") {
            install(HibiraWaitingRoom) { room = "presale" }
            get { call.respondText("You are number ${call.waitingRoomPass!!.number()}") }
        }
    }
}
```

## 开始之前

- 应用密钥只会在创建应用或轮换密钥时显示一次。请只放在服务器上，不要放进浏览器或游戏的构建包。
- Free 套餐需要把页面加载的每个来源都加入应用的“允许的来源”（开发时是 http://localhost:3000，之后是您的域名）。"*" 需要付费套餐。

- [Maven Central](https://central.sonatype.com/artifact/io.hibira/hibira-ktor)
- [GitHub](https://github.com/hibira-io/kotlin)
