| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- --- 模块功能:socket长连接功能测试.
- -- 与服务器连接成功后
- --
- -- 每隔10秒钟发送一次"heart data\r\n"字符串到服务器
- --
- -- 每隔20秒钟发送一次"location data\r\n"字符串到服务器
- --
- -- 与服务器断开连接后,会自动重连
- -- @author openLuat
- -- @module socketLongConnection.testSocket1
- -- @license MIT
- -- @copyright openLuat
- -- @release 2018.03.27
- module(..., package.seeall)
- require "socket"
- require "socketOutMsg"
- require "socketInMsg"
- local ready = false
- --- socket连接是否处于激活状态
- -- @return 激活状态返回true,非激活状态返回false
- -- @usage socketTask.isReady()
- function isReady()
- return ready
- end
- -- demo使用的供电函数,测试时请根据自己的供电方式设置
- local function power(enable)
- if enable then
- pmd.ldoset(15, pmd.LDO_VMMC)
- else
- pmd.ldoset(0, pmd.LDO_VMMC)
- end
- end
- local para = {
- powerFunc = power
- }
- -- 启动socket客户端任务
- sys.taskInit(function()
- local retryConnectCnt = 0
- while true do
- link.openNetwork(link.ESP8266, para)
- if not socket.isReady() then
- retryConnectCnt = 0
- -- 等待网络环境准备就绪,超时时间是5分钟
- sys.waitUntil("IP_READY_IND", 300000)
- end
- if socket.isReady() then
- -- 创建一个socket tcp客户端
- local socketClient = socket.tcp()
- -- 阻塞执行socket connect动作,直至成功
- if socketClient:connect("112.125.89.8", "33871") then
- retryConnectCnt = 0
- ready = true
- socketOutMsg.init()
- -- 循环处理接收和发送的数据
- while true do
- if not socketInMsg.proc(socketClient) then
- log.error("socketTask.socketInMsg.proc error")
- break
- end
- if not socketOutMsg.proc(socketClient) then
- log.error("socketTask.socketOutMsg proc error")
- break
- end
- end
- socketOutMsg.unInit()
- ready = false
- else
- retryConnectCnt = retryConnectCnt + 1
- end
- -- 断开socket连接
- socketClient:close()
- if retryConnectCnt >= 5 then
- link.shut()
- retryConnectCnt = 0
- end
- sys.wait(5000)
- else
- -- 进入飞行模式,20秒之后,退出飞行模式
- net.switchFly(true)
- sys.wait(20000)
- net.switchFly(false)
- end
- end
- end)
|