sys.taskInit( function() local retryConnectCnt=0 while true do if not socket.isReady()…
- User
sys.taskInit( function() local retryConnectCnt=0 while true do 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( “180.97.81.180” "50368"1 then retryConnectCnt= ready = true socketoutMsg.init() –循环处理接收和发送的数据 while true do if not socketInMsg.proc(socketClient) then log.error("socketTask.socket if not socketoutMsg.proc(socketClient) then log.error("socketTask.socke end socketoutMsg.unInit() function sndHeart() insertMsg(“heart data\r\n”,{cb=sndHeartCb}) end — socket客户端数据发送处理 – @param socketClient,socket客户端对象 – @return 处理成功返回true,处理出错返回false – @usage socketOutMsg.proc(socketClient) function proc(socketClient) while #msgQueue>0 do local outMsg = table.remove(msgQueue,1) local result = socketClient:send(outMsg.data) if outMsg.user and outMsg.user.cb then outMsg.user.cb(result,outMsg.user.para) end if not result then return end end return true end — socket客户端数据接收处理 – @param socketClient,socket客户端对象 – @return 处理成功返回true,处理出错返回false – @usage socketInMsg.proc(socketClient) function proc(socketClient) local result,data while true do result,data = socketClient:recv(60000,“APP_SOCKET_SEND_DATA”) --接收到数据 if result then log.info(“socketInMsg.proc”,data)
--TODO:根据需求自行处理data
else break end end
return result or data==“timeout” or data==“APP_SOCKET_SEND_DATA” 编写代码使其完整
- Assistant
你好,下面是根据给出的代码片段来完整编写代码:
local retryConnectCnt = 0 sys.taskInit(function() while true do 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("180.97.81.180", "50368") then retryConnectCnt = 0 ready = true socketoutMsg.init() --循环处理接收和发送的数据 while true do if not socketInMsg.proc(socketClient) then log.error("socketTask.socketInMsg.proc error") end if not socketoutMsg.proc(socketClient) then log.error("socketTask.socketoutMsg.proc error") end end else retryConnectCnt = retryConnectCnt + 1 if retryConnectCnt == 3 then log.error("Failed to connect to server") break end sys.wait(3000) --等待3秒后重试连接 end end end socketoutMsg.unInit() end) function sndHeart() insertMsg("heart data\r\n", { cb = sndHeartCb }) end function socketoutMsg.proc(socketClient) while #msgQueue > 0 do local outMsg = table.remove(msgQueue, 1) local result = socketClient:send(outMsg.data) if outMsg.user and outMsg.user.cb then outMsg.user.cb(result, outMsg.user.para) end if not result then return false end end return true end function socketInMsg.proc(socketClient) local result, data while true do result, data = socketClient:recv(60000, "APP_SOCKET_SEND_DATA") --接收到数据 if result then log.info("socketInMsg.proc", data) --TODO:根据需求自行处理data else break end end return result or data == "timeout" or data == "APP_SOCKET_SEND_DATA" end - User
[2023-08-02 13:21:00.119][000000000.294] E/user.coroutine.resume main.lua:26: attempt to call a nil value (field 'isReady') stack traceback: main.lua:26: in function main.lua:24 [2023-08-02 13:21:00.129][000000000.295] E/main Luat: [2023-08-02 13:21:00.132][000000000.295] E/main main.lua:68: attempt to index a nil value (global 'socketoutMsg')
- Assistant