Commit 1261c590 by 王宇航

1.7.3

將消息處理過程放到協程作用域中,可以做一些接口操作
parent 031cee35
...@@ -55,7 +55,7 @@ afterEvaluate { ...@@ -55,7 +55,7 @@ afterEvaluate {
groupId = "com.gingersoft.connect" groupId = "com.gingersoft.connect"
artifactId = "Connect" artifactId = "Connect"
version = "1.7.2" version = "1.7.3"
// 添加 POM 配置 // 添加 POM 配置
// pom { // pom {
// name.set("GingerSoftConnect") // name.set("GingerSoftConnect")
......
...@@ -10,7 +10,7 @@ object MsgParser { ...@@ -10,7 +10,7 @@ object MsgParser {
val parserMap by lazy { mutableMapOf<String, MsgProcess>() } val parserMap by lazy { mutableMapOf<String, MsgProcess>() }
fun process(msgBean: MessageBean?): SocketCallbackBean? { suspend fun process(msgBean: MessageBean?): SocketCallbackBean? {
return if (msgBean != null) { return if (msgBean != null) {
val result = parserMap[msgBean.action.toString()]?.processMsg(msgBean.data) val result = parserMap[msgBean.action.toString()]?.processMsg(msgBean.data)
if (result != null) { if (result != null) {
......
...@@ -7,5 +7,5 @@ interface MsgProcess { ...@@ -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 ...@@ -2,6 +2,9 @@ package com.gingersoft.connect.utils
import android.util.Log import android.util.Log
import com.gingersoft.connect.bean.MessageBean 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.WebSocket
import org.java_websocket.handshake.ClientHandshake import org.java_websocket.handshake.ClientHandshake
import org.java_websocket.server.WebSocketServer import org.java_websocket.server.WebSocketServer
...@@ -30,9 +33,14 @@ class MyWebSocketServer(address: InetSocketAddress) : ...@@ -30,9 +33,14 @@ class MyWebSocketServer(address: InetSocketAddress) :
// RicePOS收到KDS要獲取食品的消息後,發送本地食品信息過去 // RicePOS收到KDS要獲取食品的消息後,發送本地食品信息過去
message?.let { message?.let {
val msg = GsonUtils.GsonToBean(message, MessageBean::class.java) val msg = GsonUtils.GsonToBean(message, MessageBean::class.java)
MsgParser.process(msg)?.let { // 创建一个协程作用域
// 接受到消息並處理後,將結果返回回去 val scope = CoroutineScope(Dispatchers.IO)
conn?.send(GsonUtils.GsonString(it)) // 在协程作用域中异步处理消息
scope.launch {
MsgParser.process(msg)?.let {
// 接受到消息並處理後,將結果返回回去
conn?.send(GsonUtils.GsonString(it))
}
} }
} }
} }
......
...@@ -4,7 +4,7 @@ import com.gingersoft.connect.bean.ResultBean ...@@ -4,7 +4,7 @@ import com.gingersoft.connect.bean.ResultBean
import com.gingersoft.connect.bean.SoldOutMsgBean import com.gingersoft.connect.bean.SoldOutMsgBean
interface SoldOutMsgProcess : MsgProcess { interface SoldOutMsgProcess : MsgProcess {
override fun processMsg(data: Any?): ResultBean { override suspend fun processMsg(data: Any?): ResultBean {
val gsonToBean = GsonUtils.GsonToBean(data, SoldOutMsgBean::class.java) val gsonToBean = GsonUtils.GsonToBean(data, SoldOutMsgBean::class.java)
if (gsonToBean == null) { if (gsonToBean == null) {
return ResultBean(false, "解析失敗") return ResultBean(false, "解析失敗")
...@@ -12,6 +12,6 @@ interface SoldOutMsgProcess : MsgProcess { ...@@ -12,6 +12,6 @@ interface SoldOutMsgProcess : MsgProcess {
return processSoldOutMsg(gsonToBean) 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