Skip to content

Instantly share code, notes, and snippets.

@cablehead
Created December 10, 2015 19:45
Show Gist options
  • Save cablehead/0a7ec810fe095d1106bb to your computer and use it in GitHub Desktop.
Save cablehead/0a7ec810fe095d1106bb to your computer and use it in GitHub Desktop.
local levee = require("levee")
local h = levee.Hub()
local connections = 0
-- HTTP endpoint to see number of connections
h:spawn(function()
local err, http = h.http:listen(8000)
while true do
local err, conn = http:recv()
if err then break end
local err, req = conn:recv()
req.response:send({
levee.HTTPStatus(200),
{},
("Connected: %s\n"):format(connections)})
conn:close()
end
end)
-- TCP Service
local err, serve = h.tcp:listen(9000)
while true do
local err, conn = serve:recv()
h:spawn(function()
connections = connections + 1
while true do
local err = conn:write("hi\n")
if err then break end
h:sleep(1000)
end
connections = connections - 1
conn:close()
end)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment