Skip to content

Commit

Permalink
add more interface and split entity
Browse files Browse the repository at this point in the history
  • Loading branch information
RTAkland committed Jan 5, 2025
1 parent 92c47b2 commit d67f032
Show file tree
Hide file tree
Showing 18 changed files with 48 additions and 19 deletions.
2 changes: 1 addition & 1 deletion api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ repositories {

dependencies {
api(libs.java.websocket)
api(project(":server"))
api(project(":common"))
}
11 changes: 8 additions & 3 deletions api/src/main/kotlin/cn/rtast/kwsify/Kwsify.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ class Kwsify(private val address: String) : IOperation {
private val subscribers = mutableMapOf<String, MutableList<Subscriber>>()
private val executor = Executors.newSingleThreadScheduledExecutor()

private fun startHeartbeat() {
private fun startHeartbeat(channel: String) {
executor.scheduleAtFixedRate({
val packet = HeartbeatPacket(OPCode.HEARTBEAT).toByteArray()
websocket.send(packet)
subscribers[channel]?.forEach { it.onHeartbeat(channel) }
}, 0, 10, TimeUnit.SECONDS)
}

Expand All @@ -35,15 +36,19 @@ class Kwsify(private val address: String) : IOperation {
override fun onOpen(handshakedata: ServerHandshake) {
val authPacket = SubscribePacket(OPCode.JOIN, UUID.randomUUID(), channel, broadcastSelf).toByteArray()
websocket.send(authPacket)
startHeartbeat()
subscribers[channel]?.forEach { it.onOpen(channel) }
startHeartbeat(channel)
}

override fun onMessage(message: String) {
}

override fun onMessage(bytes: ByteBuffer) {
val opcodePacket = OPCodePacket.fromByteArray(bytes.duplicate())
if (opcodePacket.op == OPCode.HEARTBEAT_REPLY) return
if (opcodePacket.op == OPCode.HEARTBEAT_REPLY) {
subscribers[channel]?.forEach { it.onHeartbeatReply(channel) }
return
}
val packet = OutboundMessageBytesPacket.fromByteArray(bytes)
val channel = packet.channel
subscribers[channel]?.forEach { subscriber ->
Expand Down
19 changes: 18 additions & 1 deletion api/src/main/kotlin/cn/rtast/kwsify/Subscriber.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,28 @@ import cn.rtast.kwsify.entity.OutboundMessageBytesPacket
interface Subscriber {
/**
* 接收到二进制消息时
* [payload] 为解析后的数据类实体
* [rawPacket] 为原始二进制数据包
*/
fun onMessage(channel: String, payload: ByteArray, packet: OutboundMessageBytesPacket) {}
fun onMessage(channel: String, rawPacket: ByteArray, payload: OutboundMessageBytesPacket)

/**
* websocket连接断开时
*/
fun onClosed(channel: String)

/**
* websocket连接打开时
*/
fun onOpen(channel: String)

/**
* 当客户端发送心跳包时
*/
fun onHeartbeat(channel: String) {}

/**
* 当接收到服务端返回的心跳包时
*/
fun onHeartbeatReply(channel: String) {}
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
/*
* Copyright © 2024 RTAkland
* Copyright © 2025 RTAkland
* Author: RTAkland
* Date: 2024/11/30
* Date: 2025/1/5
*/


package test.kwsify
package kws

import cn.rtast.kwsify.Kwsify
import cn.rtast.kwsify.Subscriber
import cn.rtast.kwsify.entity.OutboundMessageBytesPacket


fun main() {
val wsify = Kwsify("ws://127.0.0.1:8080")
wsify.subscribe("test", true, object : Subscriber {

override fun onMessage(channel: String, payload: ByteArray, packet: OutboundMessageBytesPacket) {
override fun onMessage(channel: String, rawPacket: ByteArray, packet: OutboundMessageBytesPacket) {
println(String(packet.body))
}

override fun onClosed(channel: String) {
println("closed")
wsify.reconnect()
}

override fun onOpen(channel: String) {

}
})
Thread.sleep(1000L)
while (true) {
wsify.publish("test", "114514")
Thread.sleep(1000)
}
}
}
Empty file added common/build.gradle.kts
Empty file.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Copyright © 2025 RTAkland
* Author: RTAkland
* Date: 2025/1/3
* Date: 2025/1/5
*/


Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
kotlin.code.style=official
appVersion=2.0.0
libVersion=2.0.0
appVersion=2.0.1
libVersion=2.0.1

#systemProp.http.proxyHost=127.0.0.1
#systemProp.http.proxyPort=12334
Expand Down
1 change: 1 addition & 0 deletions server/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ plugins {
}

dependencies {
implementation(project(":common"))
implementation(libs.gson)
implementation(libs.kotlinx.cli)
implementation(libs.java.websocket)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Copyright © 2024 RTAkland
* Copyright © 2025 RTAkland
* Author: RTAkland
* Date: 2024/12/1
* Date: 2025/1/5
*/

@file:JvmName("ConnectionState")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/*
* Copyright © 2024 RTAkland
* Copyright © 2025 RTAkland
* Author: RTAkland
* Date: 2024/11/30
* Date: 2025/1/5
*/


package test
package kws

import cn.rtast.kwsify.util.KWsifyServer

fun main() {
KWsifyServer(8080).start()
}
}
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ rootProject.name = "kwsify"

include(":api")
include(":server")
include("common")

0 comments on commit d67f032

Please sign in to comment.