demo.lua 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. --- 模块功能:websocket客户端
  2. -- @module websocket
  3. -- @author OpenLuat
  4. -- @license MIT
  5. -- @copyright OpenLuat.com
  6. -- @release 2021.04.08
  7. require "utils"
  8. require "pm"
  9. require "websocket"
  10. module(..., package.seeall)
  11. -- -- 创建 websocket 对象
  12. local ws = websocket.new("ws://121.40.165.18:8800")
  13. ws:on("open", function()
  14. ws:send("hello websocket server!")
  15. end)
  16. ws:on("message", function(msg)
  17. log.info("收到 websocket server 的消息:", msg)
  18. end)
  19. ws:on("sent", function()
  20. log.info("sent to websocket:", "发送消息已完成!")
  21. end)
  22. ws:on("error", function(msg)
  23. log.error("websocket error:", msg)
  24. end)
  25. ws:on("close", function(code)
  26. log.info("websocket closed,关闭码:", code)
  27. end)
  28. -- 启动任务进程
  29. sys.taskInit(ws.start, ws, 180)
  30. -- sys.taskInit(ws.start, ws, 180, function(msg)log.info("websocket:", msg) end)
  31. sys.taskInit(function ()
  32. while true do
  33. sys.wait(2000)
  34. ws:send("www.openluat.com",true)
  35. end
  36. end)
  37. sys.timerLoopStart(function()
  38. log.info("打印占用的内存:", _G.collectgarbage("count"))-- 打印占用的RAM
  39. log.info("打印可用的空间", rtos.get_fs_free_size())-- 打印剩余FALSH,单位Byte
  40. end, 10000)