telnet.lua 967 B

1234567891011121314151617181920212223242526272829303132333435
  1. -- a simple telnet server
  2. telnet_srv = net.createServer(net.TCP, 180)
  3. telnet_srv:listen(2323, function(socket)
  4. local fifo = {}
  5. local fifo_drained = true
  6. local function sender(c)
  7. if #fifo > 0 then
  8. c:send(table.remove(fifo, 1))
  9. else
  10. fifo_drained = true
  11. end
  12. end
  13. local function s_output(str)
  14. table.insert(fifo, str)
  15. if socket ~= nil and fifo_drained then
  16. fifo_drained = false
  17. sender(socket)
  18. end
  19. end
  20. node.output(s_output, 0) -- re-direct output to function s_ouput.
  21. socket:on("receive", function(c, l)
  22. node.input(l) -- works like pcall(loadstring(l)) but support multiple separate line
  23. end)
  24. socket:on("disconnection", function(c)
  25. node.output(nil) -- un-regist the redirect output function, output goes to serial
  26. end)
  27. socket:on("sent", sender)
  28. print("Welcome to NodeMCU world.")
  29. end)