socketOutMsg.lua 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 msgQuene = {}
  10. local function insertMsg(data,user)
  11. table.insert(msgQuene,{data=data,user=user})
  12. sys.publish("APP_SOCKET_SEND_DATA")
  13. end
  14. local function sndGetCb(result)
  15. log.info("socketOutMsg.sndGetCb",result)
  16. if result then sys.timerStart(sndGet,20000) end
  17. end
  18. function sndGet()
  19. insertMsg("GET / HTTP/1.1\r\nHost: 36.7.87.100\r\nConnection: keep-alive\r\n\r\n",{cb=sndGetCb})
  20. end
  21. --- 初始化“socket客户端数据发送”
  22. -- @return 无
  23. -- @usage socketOutMsg.init()
  24. function init()
  25. sndGet()
  26. end
  27. --- 去初始化“socket客户端数据发送”
  28. -- @return 无
  29. -- @usage socketOutMsg.unInit()
  30. function unInit()
  31. sys.timerStop(sndGet)
  32. while #msgQuene>0 do
  33. local outMsg = table.remove(msgQuene,1)
  34. if outMsg.user and outMsg.user.cb then outMsg.user.cb(false,outMsg.user.para) end
  35. end
  36. end
  37. --- socket客户端数据发送处理
  38. -- @param socketClient,socket客户端对象
  39. -- @return 处理成功返回true,处理出错返回false
  40. -- @usage socketOutMsg.proc(socketClient)
  41. function proc(socketClient)
  42. while #msgQuene>0 do
  43. local outMsg = table.remove(msgQuene,1)
  44. local result = socketClient:send(outMsg.data)
  45. if outMsg.user and outMsg.user.cb then outMsg.user.cb(result,outMsg.user.para) end
  46. if not result then return end
  47. end
  48. return true
  49. end