* * description: 发送数据到各端 * author: kenan * created on: 2021/4/8 11.03 * param key: business--表示发送给业务服务器,logic--表示逻辑服务器 * param Actioncode: 发送指令 * param ModID: 站点ID(3位前补零)+服务器模块类型(2位前补零),示例为1号站点服务器的数据服务 * param Token: 登录token * param Data: 发送的具体数据 * return return_1: */
(key, Actioncode, ModID, Token string, Data interface{})
| 264 | * return return_1: |
| 265 | */ |
| 266 | func SendAndReceive(key, Actioncode, ModID, Token string, Data interface{}) (data baseMsg, code int) { |
| 267 | if httpReqMsgReceiver.maxReceiver < len(httpReqMsgReceiver.receiver) { |
| 268 | return data, 51002 |
| 269 | } |
| 270 | |
| 271 | cmd := httpReqMsgReceiver.getCmd() |
| 272 | ch := make(chan []byte) |
| 273 | pErr := httpReqMsgReceiver.pushMap(cmd, ch) |
| 274 | |
| 275 | defer func() { |
| 276 | httpReqMsgReceiver.deleteMap(cmd) |
| 277 | httpReqMsgReceiver.recoverCmd(cmd) |
| 278 | }() |
| 279 | |
| 280 | if pErr != 200 { |
| 281 | return data, 51004 |
| 282 | } |
| 283 | |
| 284 | msg := baseMsg{ |
| 285 | Company: "SmartIDE", |
| 286 | Actioncode: Actioncode, |
| 287 | Data: Data, |
| 288 | ModID: ModID, |
| 289 | Token: Token, |
| 290 | } |
| 291 | |
| 292 | msgByte, _ := json.Marshal(msg) |
| 293 | wsclient.mtx.Lock() |
| 294 | if clientArr[key] != nil { |
| 295 | sErr := clientArr[key].WriteMessage(websocket.BinaryMessage, msgByte) |
| 296 | defer wsclient.mtx.Unlock() |
| 297 | if sErr != nil { |
| 298 | return data, 51004 |
| 299 | } |
| 300 | |
| 301 | select { |
| 302 | case res := <-ch: |
| 303 | var msg baseMsg |
| 304 | err := json.Unmarshal([]byte(res), &msg) |
| 305 | if err != nil { |
| 306 | WSLog("数据解析失败") |
| 307 | return data, 51004 |
| 308 | } |
| 309 | SmartIDELog.ConsoleDebug(fmt.Sprintf("服务器推送数据:%+v\n", msg)) |
| 310 | return msg, msg.Result |
| 311 | case <-time.After(httpReqMsgReceiver.receiveMsgTimeout): |
| 312 | SmartIDELog.ConsoleDebug(fmt.Sprintf("接收发送数据:连接超时\n")) |
| 313 | WSLog("连接超时") |
| 314 | return data, 51005 |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | defer wsclient.mtx.Unlock() |
| 319 | return data, 51006 |
| 320 | } |
| 321 | |
| 322 | /* |
| 323 | * description: ws接口日志记录器 |
no test coverage detected