socketTask.lua 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. --- 模块功能:socket长连接功能测试.
  2. -- 与服务器连接成功后
  3. --
  4. -- 每隔10秒钟发送一次"heart data\r\n"字符串到服务器
  5. --
  6. -- 每隔20秒钟发送一次"location data\r\n"字符串到服务器
  7. --
  8. -- 与服务器断开连接后,会自动重连
  9. -- @author openLuat
  10. -- @module socketLongConnection.testSocket1
  11. -- @license MIT
  12. -- @copyright openLuat
  13. -- @release 2018.03.27
  14. module(..., package.seeall)
  15. require "link"
  16. require "socket"
  17. require "socketOutMsg"
  18. require "socketInMsg"
  19. require "http"
  20. local ready = false
  21. --- socket连接是否处于激活状态
  22. -- @return 激活状态返回true,非激活状态返回false
  23. -- @usage socketTask.isReady()
  24. function isReady()
  25. return ready
  26. end
  27. local date = {
  28. mode = 1, -- 1表示客户端;2表示服务器;默认为1
  29. intPin = pio.P0_22, -- 以太网芯片中断通知引脚
  30. rstPin = pio.P0_21, -- 复位以太网芯片引脚
  31. powerFunc=function ( state )
  32. if state then
  33. local setGpioFnc_TX = pins.setup(pio.P0_7, 0)
  34. pmd.ldoset(15, pmd.LDO_VMMC)
  35. else
  36. pmd.ldoset(0, pmd.LDO_VMMC)
  37. local setGpioFnc_TX = pins.setup(pio.P0_7, 1)
  38. end
  39. end,
  40. spi = {spi.SPI_1, 0, 0, 8, 800000} -- SPI通道参数,id,cpha,cpol,dataBits,clock,默认spi.SPI_1,0,0,8,800000
  41. }
  42. -- 启动socket客户端任务
  43. sys.taskInit(function()
  44. local retryConnectCnt = 0
  45. sys.wait(6000)
  46. link.openNetwork(link.CH395, date)
  47. while true do
  48. if not socket.isReady() then
  49. retryConnectCnt = 0
  50. -- 等待网络环境准备就绪,超时时间是5分钟
  51. sys.waitUntil("IP_READY_IND", 300000)
  52. end
  53. if socket.isReady() then
  54. -- 创建一个socket tcp客户端
  55. local socketClient = socket.tcp()
  56. -- 阻塞执行socket connect动作,直至成功
  57. if socketClient:connect("112.125.89.8", "34467") then
  58. retryConnectCnt = 0
  59. ready = true
  60. socketOutMsg.init()
  61. -- 循环处理接收和发送的数据
  62. while true do
  63. if not socketInMsg.proc(socketClient) then
  64. log.error("socketTask.socketInMsg.proc error")
  65. break
  66. end
  67. if not socketOutMsg.proc(socketClient) then
  68. log.error("socketTask.socketOutMsg proc error")
  69. break
  70. end
  71. end
  72. socketOutMsg.unInit()
  73. ready = false
  74. else
  75. retryConnectCnt = retryConnectCnt + 1
  76. end
  77. -- 断开socket连接
  78. log.info('socket close')
  79. socketClient:close()
  80. if retryConnectCnt >= 5 then
  81. link.shut()
  82. retryConnectCnt = 0
  83. end
  84. sys.wait(5000)
  85. else
  86. link.closeNetWork()
  87. sys.wait(20000)
  88. link.openNetwork(link.CH395, date)
  89. end
  90. end
  91. end)
  92. -- sys.taskInit(function ( )
  93. -- while true do
  94. -- sys.wait(30000)
  95. -- log.info('开始下载')
  96. -- http.request("GET","http://cdn.openluat-luatcommunity.openluat.com/attachment/20211208190511374_1.zip",nil,nil,nil,30000,cbFncFile,"123.zip")
  97. -- sys.wait(60000)
  98. -- end
  99. -- end)
  100. sys.timerLoopStart(function ()
  101. log.info("打印占用的内存:", _G.collectgarbage("count"))
  102. end,5000)