# Ktor 的即時、等候室與聊天

> 以 Kotlin 撰寫的 Ktor 3 plugin：提供 pusher-js 與 Laravel Echo 需要的頻道授權與 webhook 路由、等候室路由 plugin，並以協程使用事件、Chat API 與 Approvals。

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

以 Kotlin 撰寫的 Ktor 3 plugin：提供 pusher-js 與 Laravel Echo 需要的頻道授權與 webhook 路由、等候室路由 plugin，並以協程使用事件、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)
