testSocket.lua 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. module(..., package.seeall)
  2. require "socket"
  3. require "http"
  4. local data = {
  5. mode = 2, -- 1表示客户端;2表示服务器;默认为1
  6. clientNum = 3, -- server可连路数
  7. intPin = pio.P0_22, -- 以太网芯片中断通知引脚
  8. rstPin = pio.P0_21, -- 复位以太网芯片引脚
  9. localPort = 3352, -- 做服务器应用时,本机的端口
  10. localAddr = "192.168.31.111", -- 本机的地址
  11. localGateway = "192.168.31.1", -- 本机的网关地址
  12. CH395MAC = "84C2E4A81124",
  13. powerFunc = function(state)
  14. if state then
  15. local setGpioFnc_TX = pins.setup(pio.P0_7, 0)
  16. pmd.ldoset(15, pmd.LDO_VMMC)
  17. else
  18. pmd.ldoset(0, pmd.LDO_VMMC)
  19. local setGpioFnc_TX = pins.setup(pio.P0_7, 1)
  20. end
  21. end,
  22. spi = {spi.SPI_1, 0, 0, 8, 800000} -- SPI通道参数,id,cpha,cpol,dataBits,clock,默认spi.SPI_1,0,0,8,800000
  23. }
  24. -- 创建server
  25. local serverOn = {}
  26. local a = true
  27. sys.taskInit(function()
  28. sys.wait(3000)
  29. while not link.openNetwork(link.CH395, data) do
  30. sys.wait(5000)
  31. end
  32. while true do
  33. while not socket.isReady() do
  34. sys.wait(1000)
  35. end
  36. local socketServer = socket.tcp(nil, nil,{type = "TCPSERVER"} )
  37. --{type = "TCPSERVER"}
  38. --"TCPSERVER"
  39. while socketServer:serverSelect() do
  40. end
  41. socketServer:close()
  42. serverOn={}
  43. end
  44. end)
  45. --关闭连接示例,打开后,会60秒后断开第一个客户端连接
  46. -- sys.timerStart(function ()
  47. -- --关闭server或者server连接
  48. -- serverOn[1]:serverClose()
  49. -- end, 60000)
  50. --server被连接"tcpServer"消息会传值para,利用这个值创建server连接对象。
  51. sys.subscribe("tcpServer", function(para)
  52. sys.taskInit(function()
  53. local socketClient
  54. while not socketClient do
  55. socketClient = socket.tcp(nil, nil, para)
  56. end
  57. serverOn[socketClient.id] = socketClient
  58. while socketClient:serverSelect() do
  59. end
  60. log.info('close',socketClient.id)
  61. serverOn[socketClient.id] = nil
  62. socketClient:close()
  63. end)
  64. end)
  65. -- 测试代码,用于发送消息给socket
  66. sys.taskInit(function()
  67. local dd = 0
  68. while true do
  69. while not socket.isReady() do
  70. sys.wait(2000)
  71. end
  72. sys.wait(1000)
  73. local num = 0
  74. for i, v in pairs(serverOn) do
  75. if serverOn[i] then
  76. num = num + 1
  77. serverOn[i]:serverSend("hello word" .. dd.."\r\n", 20)
  78. sys.wait(500)
  79. end
  80. end
  81. log.info('server client number', num)
  82. dd = dd + 1
  83. end
  84. end)
  85. -- 测试代码,用于从socket接收消息
  86. sys.taskInit(function()
  87. local cnt = 0
  88. while not socket.isReady() do
  89. sys.wait(2000)
  90. end
  91. sys.wait(10000)
  92. -- 这是演示用异步接口直接读取服务器数据
  93. while true do
  94. for i, v in ipairs(serverOn) do
  95. if serverOn[i] then
  96. local data = serverOn[i]:serverRecv()
  97. if data ~= '' then
  98. cnt = cnt + #data
  99. log.info("客户端"..serverOn[i].id.."发来数据:", cnt, data:sub(1, 30))
  100. end
  101. end
  102. sys.wait(100)
  103. end
  104. sys.wait(100)
  105. end
  106. end)
  107. ---------Http------------
  108. --不属于server功能,可注释
  109. local function cbFnc(result,prompt,head,body)
  110. log.info("testHttp.cbFnc",result,prompt)
  111. if result and head then
  112. for k,v in pairs(head) do
  113. log.info("testHttp.cbFnc",k..": "..v)
  114. end
  115. end
  116. if result and body then
  117. log.info("testHttp.cbFnc","bodyLen="..body:len())
  118. end
  119. end
  120. local function cbFncFile(result,prompt,head,filePath)
  121. log.info("testHttp.cbFncFile",result,prompt,filePath)
  122. if result and head then
  123. for k,v in pairs(head) do
  124. log.info("testHttp.cbFncFile",k..": "..v)
  125. end
  126. end
  127. if result and filePath then
  128. local size = io.fileSize(filePath)
  129. log.info("testHttp.cbFncFile","fileSize="..size)
  130. --输出文件内容,如果文件太大,一次性读出文件内容可能会造成内存不足,分次读出可以避免此问题
  131. if size<=4096 then
  132. log.info("testHttp.cbFncFile",io.readFile(filePath))
  133. else
  134. end
  135. end
  136. --文件使用完之后,如果以后不再用到,需要自行删除
  137. if filePath then os.remove(filePath) end
  138. end
  139. --作为server,最多开启7路。如果开启5路server通道,那还剩2通道可做客户端使用。(如果要解析域名建议留2客户端 通道)
  140. sys.taskInit(function ()
  141. while true do
  142. sys.wait(30000)
  143. http.request("GET","http://www.lua.org",nil,nil,nil,nil,cbFnc)
  144. log.info('结束')
  145. sys.wait(30000)
  146. --return
  147. end
  148. end)