Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
RTAkland committed Sep 13, 2023
1 parent 606b868 commit d27a62a
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 15 deletions.
35 changes: 27 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,35 @@

# 使用

> [Release](https://github.com/RTAkland/kwsify/releases/latest/)中下载最新版本的jar
> 然后使用`java -jar kwsify.jar`运行, 默认监听地址为`0.0.0.0:5050`使用命令行参数来修改默认值
> `java -jar kwsify.jar 127.0.0.1 6666` 这个启动方式将监听地址设置为了127.0.0.1:6666
```shell
$ java -jar kwsify.jar --help # 获取帮助
```

> 客户端连接需要使用websocket客户端连接, 你可以使用`wscat`来简单的调试, 使用`npm install -g wscat`
> 来安装wscat, 安装完成后使用`wscat -c ws://localhost:5050/subscribe`然后发送`{"action":"subscribe","channel":"test"}`
> 来订阅`test`频道, 客户端任何原因失去连接都会使其取消订阅, 再开启一个ws客户端连接到`/publish`端点
> 发送`{"channel":"test","payload":"this is a test message"}`然后
> 即可在第一个客户端的消息列表收到`{"channel":"test","payload":"this is a test message"}`
> [Release](https://github.com/RTAkland/kwsify/releases/latest/)中下载最新版本的jar使用以下命令运行
```shell
$ java -jar kwsify.jar [--host 0.0.0.0] [--port 6060]
```

## Subscribe

> 使用Websocket连接到服务器
```shell
$ # 订阅者
$ wscat -c ws://localhost:5050/subscribe
$ {"action":"Subscribe", "channel":"channel-1"}
$ # 取消订阅
$ {"action":"Unsubscribe", "channel":"channel-1"}
```

```shell
$ # 发布者
$ wscat -c ws://localhost:5050/publish
$ {"action":"Publish", "channel":"channel-1", "payload":"this is a test message"}
```

> 然后就会在订阅者的消息列表中找到`this is a test message`
# 开源

Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group = "cn.rtast"
version = "0.0.1"
version = "0.0.2-beta"

repositories {
mavenCentral()
Expand Down
17 changes: 13 additions & 4 deletions src/main/kotlin/cn/rtast/kwsify/Kwsify.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,22 @@ class Kwsify(address: InetSocketAddress) : WebSocketServer(address) {
} else if (action == ActionType.Subscribe && endpoint == "subscribe") {
SubscribeEndpoint().onMessage(conn, jsonPayload)
} else if (action == ActionType.Unsubscribe && endpoint == "subscribe") {
sessions.forEachIndexed { i, s ->
if (s.session == conn) {
sessions.removeAt(i)
try {
if (!sessions.contains(Session(jsonPayload.channel, conn))) {
conn.send("Already unsubscribed!")
} else {
sessions.forEachIndexed { i, s ->
if (s.session == conn) {
sessions.removeAt(i)
}
}
}
} catch (_: ConcurrentModificationException) {
println("Removed")
}
} else {
println("Unknown operation.")
conn.send("Unknown operation!")
println("Unknown operation!")
}
} catch (_: JsonSyntaxException) {
conn.send("Json Syntax exception!")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class PublishEndpoint {
Kwsify.sessions.forEach {
it.session.send(message.payload)
}
conn.send("Published!")
} catch (_: JsonSyntaxException) {
conn.send("Json syntax exception!")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class SubscribeEndpoint {
if (message.action == ActionType.Subscribe) {
if (!Kwsify.sessions.contains(session)) {
Kwsify.sessions.add(session)
conn.send("Subscribed!")
} else {
conn.send("This connection is already subscribed!")
}
Expand Down
2 changes: 0 additions & 2 deletions src/main/kotlin/cn/rtast/kwsify/utils/ArgumentsParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,12 @@ class ArgumentsParser(private val args: Array<String>) {
fun parse(): Config {
val host by parser.option(
ArgType.String,
shortName = "h",
fullName = "host",
description = "Host to listen"
).default("0.0.0.0")

val port by parser.option(
ArgType.Int,
shortName = "p",
fullName = "port",
description = "Port to listen"
).default(5050)
Expand Down

0 comments on commit d27a62a

Please sign in to comment.