Commit 1261c590 by 王宇航

1.7.3

將消息處理過程放到協程作用域中,可以做一些接口操作
parent 031cee35
......@@ -55,7 +55,7 @@ afterEvaluate {
groupId = "com.gingersoft.connect"
artifactId = "Connect"
version = "1.7.2"
version = "1.7.3"
// 添加 POM 配置
// pom {
// name.set("GingerSoftConnect")
......
......@@ -10,7 +10,7 @@ object MsgParser {
val parserMap by lazy { mutableMapOf<String, MsgProcess>() }
fun process(msgBean: MessageBean?): SocketCallbackBean? {
suspend fun process(msgBean: MessageBean?): SocketCallbackBean? {
return if (msgBean != null) {
val result = parserMap[msgBean.action.toString()]?.processMsg(msgBean.data)
if (result != null) {
......
......@@ -7,5 +7,5 @@ interface MsgProcess {
/**
* 消息處理
*/
fun processMsg(data: Any?): ResultBean
suspend fun processMsg(data: Any?): ResultBean
}
\ No newline at end of file
......@@ -2,6 +2,9 @@ package com.gingersoft.connect.utils
import android.util.Log
import com.gingersoft.connect.bean.MessageBean
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.java_websocket.WebSocket
import org.java_websocket.handshake.ClientHandshake
import org.java_websocket.server.WebSocketServer
......@@ -30,12 +33,17 @@ class MyWebSocketServer(address: InetSocketAddress) :
// RicePOS收到KDS要獲取食品的消息後,發送本地食品信息過去
message?.let {
val msg = GsonUtils.GsonToBean(message, MessageBean::class.java)
// 创建一个协程作用域
val scope = CoroutineScope(Dispatchers.IO)
// 在协程作用域中异步处理消息
scope.launch {
MsgParser.process(msg)?.let {
// 接受到消息並處理後,將結果返回回去
conn?.send(GsonUtils.GsonString(it))
}
}
}
}
override fun onError(conn: WebSocket?, ex: Exception?) {
Log.e(TAG, "服務器出錯: ${ex?.message}")
......
......@@ -4,7 +4,7 @@ import com.gingersoft.connect.bean.ResultBean
import com.gingersoft.connect.bean.SoldOutMsgBean
interface SoldOutMsgProcess : MsgProcess {
override fun processMsg(data: Any?): ResultBean {
override suspend fun processMsg(data: Any?): ResultBean {
val gsonToBean = GsonUtils.GsonToBean(data, SoldOutMsgBean::class.java)
if (gsonToBean == null) {
return ResultBean(false, "解析失敗")
......@@ -12,6 +12,6 @@ interface SoldOutMsgProcess : MsgProcess {
return processSoldOutMsg(gsonToBean)
}
fun processSoldOutMsg(data: SoldOutMsgBean?): ResultBean
suspend fun processSoldOutMsg(data: SoldOutMsgBean): ResultBean
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment