socketOutMsg.lua 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. --- 模块功能:socket客户端数据发送处理
  2. -- @author openLuat
  3. -- @module socketLongConnection.socketOutMsg
  4. -- @license MIT
  5. -- @copyright openLuat
  6. -- @release 2018.03.28
  7. module(...,package.seeall)
  8. --数据发送的消息队列
  9. local msgQueue = {}
  10. local function insertMsg(data,user)
  11. table.insert(msgQueue,{data=data,user=user})
  12. sys.publish("APP_SOCKET_SEND_DATA")
  13. end
  14. local function sndHeartCb(result)
  15. log.info("socketOutMsg.sndHeartCb",result)
  16. if result then sys.timerStart(sndHeart,5000) end
  17. end
  18. function sndHeart()
  19. insertMsg("heart data\r\n",{cb=sndHeartCb})
  20. end
  21. local function sndLocCb(result)
  22. log.info("socketOutMsg.sndLocCb",result)
  23. if result then sys.timerStart(sndLoc,10000) end
  24. end
  25. function sndLoc()
  26. insertMsg("location data\r\n",{cb=sndLocCb})
  27. end
  28. --- 初始化“socket客户端数据发送”
  29. -- @return 无
  30. -- @usage socketOutMsg.init()
  31. function init()
  32. --sndHeart()
  33. sndLoc()
  34. end
  35. --- 去初始化“socket客户端数据发送”
  36. -- @return 无
  37. -- @usage socketOutMsg.unInit()
  38. function unInit()
  39. sys.timerStop(sndHeart)
  40. sys.timerStop(sndLoc)
  41. while #msgQueue>0 do
  42. local outMsg = table.remove(msgQueue,1)
  43. if outMsg.user and outMsg.user.cb then outMsg.user.cb(false,outMsg.user.para) end
  44. end
  45. end
  46. --- socket客户端数据发送处理
  47. -- @param socketClient,socket客户端对象
  48. -- @return 处理成功返回true,处理出错返回false
  49. -- @usage socketOutMsg.proc(socketClient)
  50. function proc(socketClient)
  51. while #msgQueue>0 do
  52. local outMsg = table.remove(msgQueue,1)
  53. local result = socketClient:send(outMsg.data)
  54. if outMsg.user and outMsg.user.cb then outMsg.user.cb(result,outMsg.user.para) end
  55. if not result then return end
  56. end
  57. return true
  58. end