From 94a486a505a31348747dae0f0bec3479d54974a1 Mon Sep 17 00:00:00 2001 From: RTAkland Date: Sun, 4 Jan 2026 00:37:57 +0800 Subject: [PATCH] Update readme --- README-zh.md | 31 +++++++++++++++++++++++++++++-- README.md | 27 +++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/README-zh.md b/README-zh.md index 7bb2320..a3c0a4c 100644 --- a/README-zh.md +++ b/README-zh.md @@ -21,8 +21,8 @@ repositories { ```kotlin // 最新版本请前往下方链接查看 -// https://next.pkg.rtast.cn/#/releases/cn/rtast/kotlin-cfworker/kotlin-cloudflare-worker/ -implementation("cn.rtast.kotlin-cfworker:kotlin-cloudflare-worker:1.0.2") +// https://next.pkg.rtast.cn/#/releases/cn/rtast/kotlin-cfworker/core/ +implementation("cn.rtast.kotlin-cfworker:core:1.0.2") ``` ## Hello world程序 @@ -129,6 +129,33 @@ fun handleRequest(request: Request): Promise = GlobalScope.promise { 不要使用ktor来作为http客户端, 因为cloudflare worker不是一个标准的nodejs环境, 你需要使用 fetch `cn.rtast.cfworker.client.fetch` 来发送http请求, 注意不是`window.fetch`, `window.fetch`是浏览器API +# Websocket + +仅支持Websocket服务器 + +> 使用类似ktor的语法注册websocket端点 + +```kotlin +@JsExport +fun handleRequest(request: Request): Promise = GlobalScope.promise { + val server = WorkerApplication().apply { + route("/") { + respondText("Hello kotlin cloudflare worker") + } + + webSocket("/ws") { + onMessage { + println(it.readText()) + } + + onClose { + } + } + } + return@promise server.handle(request) +} +``` + # ByteArray 和 ByteBuffer 转换 ```kotlin diff --git a/README.md b/README.md index daa5431..6a666a2 100644 --- a/README.md +++ b/README.md @@ -137,6 +137,33 @@ fun handleRequest(request: Request): Promise = GlobalScope.promise { Cloudflare worker is not a standard nodejs environment, do http requests must use fetch api(not `window.fetch`, this is browser api), use `cn.rtast.cfworker.client.fetch` instead, +# Websocket + +Only websocket server is supported + +> ktor like expression + +```kotlin +@JsExport +fun handleRequest(request: Request): Promise = GlobalScope.promise { + val server = WorkerApplication().apply { + route("/") { + respondText("Hello kotlin cloudflare worker") + } + + webSocket("/ws") { + onMessage { + println(it.readText()) + } + + onClose { + } + } + } + return@promise server.handle(request) +} +``` + # ByteArray and ByteBuffer cast Kotlin cloudflare worker provided api to mutual conversion,