|
Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
-- This file:
-- http://anggtwu.net/LUA/Socket1.lua.html
-- http://anggtwu.net/LUA/Socket1.lua
-- (find-angg "LUA/Socket1.lua")
-- Author: Eduardo Ochs <eduardoochs@gmail.com>
--
-- (defun e () (interactive) (find-angg "LUA/Socket1.lua"))
-- (find-es "lua5" "luasocket-2023")
-- (find-es "lua5" "luasocket-listener")
-- (find-es "lua5" "lua-json")
-- «.test3» (to "test3")
socket = require "socket"
json = require "json"
Socket = Class {
type = "Socket",
bind = function (address, port)
return Socket { sock=socket.bind(address, port) }
end,
__index = {
test1 = function (s)
local conn = assert(s.sock:accept())
local line,err = conn:receive()
while not err do print(l); line,err = conn:receive() end
print(err)
return s
end,
--
jsondecode = function (s,j)
return json.decode.decode(j, {nothrow=true})
end,
jsonincomplete = function (s,j)
if j == "" then return "Empty" end
local o,err = s:jsondecode(j)
return err and err:match("Unclosed")
end,
receivejson = function (s)
s.conn,s.json = assert(s.sock:accept()), ""
while s:jsonincomplete(s.json) do
local line,err = s.conn:receive()
s.json = s.json..line.."\n"
end
return s:jsondecode(s.json)
end,
test2 = function (s)
end,
},
}
--[[
* (find-3EE '(eepitch-shell) '(eepitch-lua51))
* (find-3ee '(eepitch-shell) '(eepitch-lua51))
* (eepitch-shell)
* (eepitch-lua51)
dofile "Socket1.lua"
mys = Socket.bind("127.0.0.1", 5555)
mys:test1()
*
* (eepitch-shell)
f () { netcat -q 0 localhost 5555; }
g () { echo hi; sleep 1; echo bye; sleep 1; }
g | f
--]]
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "Socket1.lua"
mys = Socket {}
myj = '{"1":"foo","a":123}'
= mys:jsonincomplete("")
= mys:jsonincomplete(myj)
= mys:jsonincomplete(myj:sub(1,-2))
--]]
-- «test3» (to ".test3")
--[[
* (find-3EE '(eepitch-shell) '(eepitch-lua51))
* (find-3ee '(eepitch-shell) '(eepitch-lua51))
* (eepitch-shell)
* (eepitch-lua51)
dofile "Socket1.lua"
mys = Socket.bind("127.0.0.1", 5555)
o = mys:receivejson()
*
* (eepitch-shell)
f () { netcat -q 0 localhost 5555; }
f () { netcat localhost 5555; }
g () { for i in $*; do echo "$i"; sleep 1; done; }
# g '{"1":"foo",' '"a":123}' ' '
g '{"1":"foo",' '"a":123}' ' ' | f
*
* (eepitch-lua51)
PP(o)
mys.sock:send("ok")
mys.sock.send("ok")
= mys.sock
-- (find-es "lua5" "luasocket-2023")
{
myj = '{"1":"foo","a":123}'
--]]
-- Local Variables:
-- coding: utf-8-unix
-- End: