-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EventBus should be object for multi bot support
- Loading branch information
Showing
9 changed files
with
158 additions
and
186 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
onebot/src/main/kotlin/cn/evolvefield/onebot/client/connection/IAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package cn.evolvefield.onebot.client.connection | ||
|
||
import cn.evole.onebot.sdk.util.json.JsonsObject | ||
import cn.evolvefield.onebot.client.handler.ActionHandler | ||
import cn.evolvefield.onebot.client.handler.EventBus | ||
import cn.evolvefield.onebot.client.util.ActionSendUtils | ||
import com.google.gson.JsonSyntaxException | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.sync.Mutex | ||
import kotlinx.coroutines.sync.withLock | ||
import org.slf4j.Logger | ||
|
||
interface IAdapter { | ||
val scope: CoroutineScope | ||
val actionHandler: ActionHandler | ||
val logger: Logger | ||
|
||
fun onReceiveMessage(message: String) { | ||
try { | ||
val json = JsonsObject(message) | ||
if (json.optString(META_EVENT) != HEART_BEAT) { // 过滤心跳 | ||
logger.debug("Client received <-- {}", json.toString()) | ||
|
||
if (json.has(API_RESULT_KEY)) { // 接口回调 | ||
actionHandler.onReceiveActionResp(json) | ||
} else scope.launch { // 处理事件 | ||
mutex.withLock { | ||
EventBus.onReceive(message) | ||
} | ||
} | ||
} | ||
} catch (e: JsonSyntaxException) { | ||
logger.error("Json语法错误: {}", message) | ||
} | ||
} | ||
|
||
fun unlockMutex() { | ||
runCatching { | ||
if (mutex.isLocked) mutex.unlock() | ||
if (ActionSendUtils.mutex.isLocked) ActionSendUtils.mutex.unlock() | ||
} | ||
} | ||
|
||
companion object { | ||
private const val META_EVENT = "meta_event_type" | ||
private const val API_RESULT_KEY = "echo" | ||
private const val HEART_BEAT = "heartbeat" | ||
|
||
val mutex = Mutex() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.