123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- --- testSocket
- -- @module testSocket
- -- @author AIRM2M
- -- @license MIT
- -- @copyright openLuat.com
- -- @release 2018.10.27
- require "socket"
- module(..., package.seeall)
- -- 此处的IP和端口请填上你自己的socket服务器和端口
- local ip, port, c = "116.62.220.88", "10004"
- -- tcp test
- sys.taskInit(function()
- local r, s, p
- local recv_cnt, send_cnt = 0, 0
- while true do
- while not socket.isReady() do
- sys.wait(1000)
- end
- c = socket.tcp()
- while not c:connect(ip, port) do sys.wait(2000) end
- while true do
- r, s, p = c:recv(120000, "pub_msg")
- if r then
- recv_cnt = recv_cnt + #s
- log.info("这是收到的服务器下发的数据统计:", recv_cnt, "和前30个字节:", s:sub(1, 30))
- elseif s == "pub_msg" then
- send_cnt = send_cnt + #p
- log.info("这是收到别的线程发来的数据消息!", send_cnt, "和前30个字节", p:sub(1, 30))
- if not c:send(p) then break end
- elseif s == "timeout" then
- log.info("这是等待超时发送心跳包的显示!")
- if not c:send("ping") then break end
- else
- log.info("这是socket连接错误的显示!")
- break
- end
- end
- c:close()
- end
- end)
- -- 测试代码,用于发送消息给socket
- sys.taskInit(function()
- while not socket.isReady() do
- --sys.wait(2000)
- sys.waitUntil("IP_READY_IND",300000)
- end
- sys.wait(10000)
- -- 这是演示用sys.publish()发送数据
- for i = 1, 10 do
- sys.publish("pub_msg", string.rep("0123456789", 1024))
- sys.wait(500)
- end
- end)
- --[[
- local timerIds = {}
- sys.taskInit(function()
- sys.wait(10000)
- -- 定时器创建测试
- for i = 1, 2000 do
- local timerId
- timerId = sys.timerLoopStart(function(index)
- log.info("testSocket.timerLoopStart","index:",index,"index:",timerIds[index])
- end, 5000, i)
- if timerId then
- timerIds[i] = timerId
- end
- sys.wait(1000)
- end
- end)
- sys.timerLoopStart(function()
- log.info("打印占用的内存:", _G.collectgarbage("count"))-- 打印占用的RAM
- log.info("打印可用的空间", rtos.get_fs_free_size())-- 打印剩余FALSH,单位Byte
- end, 1000)
- ]]
- --发布消息
- sys.taskInit(function()
- sys.wait(10000)
- log.info("testSocket.pub_msg_test","start publish msg")
- -- 这是演示用sys.publish()发送数据
- for i = 1, 10 do
- sys.publish("pub_msg_test", i)
- sys.wait(500)
- end
- log.info("testSocket.pub_msg_test","publish msg finish")
- end)
- --无等待接收信息
- sys.taskInit(
- function()
- while true do
- log.info("testSocket.waitUntil.pub_msg_test1","wait pub_msg_test message ...")
- local res,data = sys.waitUntil("pub_msg_test", 3600*1000)
- if res then
- log.info("testSocket.waitUntil.pub_msg_test1","receive pub_msg_test message, data:",data)
- else
- log.info("testSocket.waitUntil.pub_msg_test1","wait pub_msg_test timeout")
- end
- end
- end)
- --有等待接收信息
- sys.taskInit(
- function()
- while true do
- log.info("testSocket.waitUntil.pub_msg_test2","wait pub_msg_test message ...")
- local res,data = sys.waitUntil("pub_msg_test", 3600*1000)
- if res then
- log.info("testSocket.waitUntil.pub_msg_test2","receive pub_msg_test message, data:",data)
- sys.wait(1000)
- else
- log.info("testSocket.waitUntil.pub_msg_test2","wait pub_msg_test timeout")
- end
- end
- end)
|