Update readme
This commit is contained in:
2 files changed
+56
-2
No files matched your search
+29
-2
@@ -21,8 +21,8 @@ repositories {
|
|||||||
|
|
||||||
```kotlin
|
```kotlin
|
||||||
// 最新版本请前往下方链接查看
|
// 最新版本请前往下方链接查看
|
||||||
// https://next.pkg.rtast.cn/#/releases/cn/rtast/kotlin-cfworker/kotlin-cloudflare-worker/
|
// https://next.pkg.rtast.cn/#/releases/cn/rtast/kotlin-cfworker/core/
|
||||||
implementation("cn.rtast.kotlin-cfworker:kotlin-cloudflare-worker:1.0.2")
|
implementation("cn.rtast.kotlin-cfworker:core:1.0.2")
|
||||||
```
|
```
|
||||||
|
|
||||||
## Hello world程序
|
## Hello world程序
|
||||||
@@ -129,6 +129,33 @@ fun handleRequest(request: Request): Promise<Response> = GlobalScope.promise {
|
|||||||
不要使用ktor来作为http客户端, 因为cloudflare worker不是一个标准的nodejs环境, 你需要使用 fetch
|
不要使用ktor来作为http客户端, 因为cloudflare worker不是一个标准的nodejs环境, 你需要使用 fetch
|
||||||
`cn.rtast.cfworker.client.fetch` 来发送http请求, 注意不是`window.fetch`, `window.fetch`是浏览器API
|
`cn.rtast.cfworker.client.fetch` 来发送http请求, 注意不是`window.fetch`, `window.fetch`是浏览器API
|
||||||
|
|
||||||
|
# Websocket
|
||||||
|
|
||||||
|
仅支持Websocket服务器
|
||||||
|
|
||||||
|
> 使用类似ktor的语法注册websocket端点
|
||||||
|
|
||||||
|
```kotlin
|
||||||
|
@JsExport
|
||||||
|
fun handleRequest(request: Request): Promise<Response> = 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 转换
|
# ByteArray 和 ByteBuffer 转换
|
||||||
|
|
||||||
```kotlin
|
```kotlin
|
||||||
|
|||||||
@@ -137,6 +137,33 @@ fun handleRequest(request: Request): Promise<Response> = GlobalScope.promise {
|
|||||||
Cloudflare worker is not a standard nodejs environment, do http requests must
|
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,
|
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<Response> = 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
|
# ByteArray and ByteBuffer cast
|
||||||
|
|
||||||
Kotlin cloudflare worker provided api to mutual conversion,
|
Kotlin cloudflare worker provided api to mutual conversion,
|
||||||
|
|||||||
Reference in New Issue
Block a user