-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinit.lua
54 lines (43 loc) · 955 Bytes
/
init.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
local socket = require "socket"
local sleep = socket.sleep
local remove = table.remove
local setmetatable = setmetatable
local assert = assert
local client = require "luaweb.client"
module "luaweb"
_M = {}
local server = _M
server.__index = server
function new(config)
local self = {
port = config.port or 80;
socket = socket.tcp();
backlog = config.backlog or 16;
callback = assert(config.callback, "callback required");
clients = {};
}
assert(self.socket:bind("*", self.port))
assert(self.socket:listen(self.backlog))
self.socket:settimeout(0)
return setmetatable(self, server)
end
function server:think()
local clients = self.clients
do
local s, err = self.socket:accept()
if s then
clients[#clients + 1] = client.new(s, self.callback)
end
end
for k = 1, #clients do
local c = clients[k]
if not c:think() then
remove(clients, k)
end
end
end
function server:run()
while true do
self:think()
end
end