💾 Archived View for gemini.conman.org › extensions › GLV-1 › handlers › sse.lua captured on 2021-12-17 at 13:26:06.
⬅️ Previous capture (2021-12-03)
-=-=-=-=-=-=-
-- ************************************************************************ -- -- Generate a simulated stream of events -- Copyright 2019 by Sean Conner. All Rights Reserved. -- -- This program is free software: you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by -- the Free Software Foundation, either version 3 of the License, or -- (at your option) any later version. -- -- This program is distributed in the hope that it will be useful, -- but WITHOUT ANY WARRANTY; without even the implied warranty of -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -- GNU General Public License for more details. -- -- You should have received a copy of the GNU General Public License -- along with this program. If not, see <http://www.gnu.org/licenses/>. -- -- Comments, questions and criticisms can be sent to: sean@conman.org -- -- ************************************************************************ -- luacheck: globals handler -- luacheck: ignore 611 631 -- https://html.spec.whatwg.org/multipage/server-sent-events.html local uuid = require "org.conman.uuid" local nfl = require "org.conman.nfl" local math = require "math" local coroutine = require "coroutine" local string = require "string" local tostring = tostring _ENV = {} local events = { "open" , "read" , "write" , "ioctl" , "close" , "error" } -- ************************************************************************ function handler(_,_,_,_,ios) local function rnddata() local max = math.random(8) + math.random(8) + math.random(8) + math.random(8) local data = "" for _ = 1 , max do data = data .. string.format("%02x",math.random(256) - 1) end return data end ios:write("20 text/event-stream\r\n") while true do if math.random() < .25 then if not ios:write("id: ",tostring(uuid()),"\r\n") then return 20 end end if math.random() < .5 then if not ios:write("event: ",events[math.random(#events)],"\r\n") then return 20 end end if math.random() < .1 then if not ios:write("retry: ",tostring(math.random(4) * 1000),"\r\n") then return 20 end end if math.random() < .1 then if not ios:write("data: ",rnddata(),"\r\n") then return 20 end end if not ios:write("data: ",rnddata(),"\r\n\r\n") then return 20 end nfl.timeout(.5 + (-1 / 4 * math.log(1 - math.random()))) -- Poisson coroutine.yield() end end -- ************************************************************************ return _ENV