-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.lua
More file actions
101 lines (86 loc) · 2.3 KB
/
server.lua
File metadata and controls
101 lines (86 loc) · 2.3 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
exports.name = "WebSocket Server"
exports.version = "0.0.1"
local net = require("net")
local wsu = require("websocketutils")
local table = require("table")
exports.new = function(func)
local t = {}
t.listener = {connect = {}, data = {}, disconnect = {}, timeout = {}}
t.clients = {}
t.on = function(self, s, c)
if self.listener[s] and type(self.listener[s]) == "table" and type(c) == "function" then
table.insert(self.listener[s], c)
end
return self
end
t.call = function(self, s, ...)
if self.listener[s] and type(self.listener[s]) == "table" then
local t = {}
for k,v in pairs(self.listener[s]) do
if type(v) == "function" then
local r = v(...)
if r then
table.insert(t, r)
end
end
end
return unpack(t)
end
end
t.server = net.createServer(function(client)
client.oldBuffer = ""
client:on("data", function(c)
client.send = function(self, msg)
client:write(wsu.assemblePacket(msg))
end
if c:sub(1,3) == "GET" then
client:write(wsu.assembleHandshakeResponse(c))
t:call("connect", client)
table.insert(t.clients, client)
for k,v in pairs(t.clients) do
if v == client then
client.id = k
end
end
else
local message, v = wsu.disassemblePacket(client.oldBuffer .. c)
if message == 3 then
client.oldBuffer = client.oldBuffer .. c
elseif message == 2 then
t:call("disconnect", client)
t.clients[client.id or 0] = nil
elseif message == 1 then
client:write(v)
client.oldBuffer = ""
elseif message then
t:call("data", client, message)
client.oldBuffer = ""
else
print("WebSocket Error: Could not parse message.")
end
end
end)
local function onTimeout()
print("Timeout")
t:call("timeout", client)
t.clients[client.id or 0] = nil
client:_end()
end
client:once('timeout', onTimeout)
process:once('exit', onTimeout)
client:setTimeout(1800000)
client:on('end', function()
t:call("disconnect", client)
t.clients[client.id or 0] = nil
process:removeListener('exit', onTimeout)
end)
end)
t.listen = function(self, ...)
t.server:listen(...)
return self
end
if type(func) == "function" then
func(t)
end
return t
end