From 6232bf5180d51f0f0eea24fb5ddc59363fe76c8c Mon Sep 17 00:00:00 2001 From: Ollo Date: Sat, 13 Feb 2021 15:11:42 +0100 Subject: [PATCH] Use table syntax to work with >lines< --- telnet.lua | 66 +++++++++++++++++++++++++++++------------------------- 1 file changed, 35 insertions(+), 31 deletions(-) diff --git a/telnet.lua b/telnet.lua index 9bac3bc..8138adb 100644 --- a/telnet.lua +++ b/telnet.lua @@ -1,36 +1,40 @@ +-- Telnet client +local telnetClient=nil +lines = {} -- Telnet Server function startTelnetServer() s=net.createServer(net.TCP, 180) s:listen(23,function(c) - global_c=c - printlines = {} - function s_output(str) - if(global_c~=nil) then - if #printlines > 0 then - printlines[ #printlines + 1] = str - else - printlines[ #printlines + 1] = str - global_c:send("\r") -- Send something, so the queue is read after sending - end - end - end - node.output(s_output, 0) - c:on("receive",function(c,l) - node.input(l) + telnetClient=c + function s_output(str) + if (telnetClient ~= nil) then + if (#lines > 0) then + table.insert(lines, str) + else + table.insert(lines, str) + telnetClient:send("\r") -- Send something, so the queue is read after sending + end + end + end + node.output(s_output, 0) + c:on("receive",function(c,l) + node.input(l) + end) + c:on("disconnection",function(c) + node.output(nil) + telnetClient=nil + end) + c:on("sent", function() + if (#lines > 0) then + local localstr = table.remove(lines, 1) + print( tostring(localstr) ) + telnetClient:send(localstr) + end + end) + print("Welcome to the Wordclock.") + print("- mydofile(\"commands\")") + print("- storeConfig()") + print("Visite https://github.com/nodemcu/nodemcu-firmware/wiki/nodemcu_api_en for further commands") end) - c:on("disconnection",function(c) - node.output(nil) - global_c=nil - end) - c:on("sent", function() - if #printlines > 0 then - global_c:send(table.remove(printlines, 1)) - end - end) - print("Welcome to the Wordclock.") - print("- mydofile(\"commands\")") - print("- storeConfig()") - print("Visite https://github.com/nodemcu/nodemcu-firmware/wiki/nodemcu_api_en for further commands") - end) - print("Telnetserver is up") -end \ No newline at end of file + print("Telnetserver started") +end