Wordclock/telnet.lua

55 lines
1.4 KiB
Lua
Raw Normal View History

2021-02-13 15:11:42 +01:00
-- Telnet client
2021-03-14 15:45:32 +01:00
telnetClient=nil
local lines = {}
-- Helper function
function s_output(str)
uart.write(0, "T:" .. tostring(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
2020-02-21 20:21:54 +01:00
-- Telnet Server
function startTelnetServer()
s=net.createServer(net.TCP, 180)
s:listen(23,function(c)
2021-02-13 15:11:42 +01:00
telnetClient=c
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
2021-03-14 13:20:38 +01:00
local line1=nil
2021-03-14 15:45:32 +01:00
for k,v in pairs(lines) do if (k==1) then
line1=v
end
uart.write(0, "Lines:" .. tostring(v) .. "\r\n")
end
2021-03-14 13:20:38 +01:00
if ( line1 ~= nil ) then
table.remove(lines, 1)
2021-03-14 15:45:32 +01:00
uart.write(0, "Telent[" .. tostring(#lines) .. "]" .. tostring(line1) .. "\r\n" )
2021-03-14 13:20:38 +01:00
telnetClient:send(line1)
end
2021-02-13 15:11:42 +01:00
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")
2021-03-14 15:45:32 +01:00
uart.write(0, "New client connected\r\n")
2020-02-21 20:21:54 +01:00
end)
2021-02-13 15:11:42 +01:00
print("Telnetserver started")
end