clients.lua

Mon, 04 Jan 2016 14:34:00 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Mon, 04 Jan 2016 14:34:00 +0000
changeset 13
c245afa537bb
parent 8
2e4c32c4fb6b
child 14
1d28dfcd9c94
permissions
-rw-r--r--

clients.lua: Fix for mark_active()

0
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local socket = require "socket";
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local server = require "net.server_select";
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local http_server = require"net.http.server";
6
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
4 local new_uuid = require "util.uuid".generate;
8
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
5 local json = require "cjson";
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
6 local formdecode = require "util.http".formdecode;
0
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local log = require "util.logger".init("clients");
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 local response_head = table.concat({
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 "HTTP/1.1 200 OK";
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 "Max-Age: 0";
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 "Expires: 0";
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 "Cache-Control: no-cache, private";
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 "Pragma: no-cache";
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 "Content-Type: multipart/x-mixed-replace; boundary=--BoundaryString";
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 "Keep-Alive: timeout=5, max=99";
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 "Connection: Keep-Alive";
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 "Transfer-Encoding: chunked";
6
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
19 "Set-Cookie: COOKIE_STRING";
0
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 "";
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 "";
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 }, "\r\n");
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 local listener = { onconnect = function () end; onincoming = function () end; }
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 local last_chunk;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27
8
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
28 local data_stream = require "eventstreams".new();
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
29
6
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
30 local have_clients = false;
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
31
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
32 -- [conn] = cookie
0
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 local clients = {};
6
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
34 -- [cookie] = conn
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
35 local client_by_cookie = {};
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
36 -- [conn] = last_active_timestamp
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
37 local active_clients = {};
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
38
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
39 local activity_timeout = 20;
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
40
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
41 local function update_have_clients()
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
42 if have_clients and not next(active_clients) then
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
43 have_clients = false;
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
44 log("debug", "No more clients");
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
45 events.fire_event("no-clients");
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
46 elseif not have_clients and next(active_clients) then
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
47 have_clients = true;
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
48 log("debug", "Active clients");
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
49 events.fire_event("have-clients");
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
50 end
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
51 end
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
52
0
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 -- Called when a HTTP stream client closes
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 function listener.ondisconnect(conn)
6
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
56 client_by_cookie[clients[conn]] = nil;
0
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 clients[conn] = nil;
6
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
58 active_clients[conn] = nil;
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
59 update_have_clients();
0
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 listener.ondetach = listener.ondisconnect;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 function handle_request(event, path)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 local path = event.request.url.path;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 if path ~= "/cam" then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 return 404;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 end
6
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
68
0
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 local conn = event.response.conn;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70
6
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
71 local cookie = event.request.headers.cookie;
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
72 if cookie then
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
73 log("debug", "Client %s connected", cookie);
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
74 else
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
75 cookie = new_uuid();
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
76 log("debug", "New client connected, assigned %s", cookie);
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
77 end
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
78 conn:write((response_head:gsub("COOKIE_STRING", cookie)));
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
79 clients[conn] = cookie;
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
80 active_clients[conn] = os.time();
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
81 client_by_cookie[cookie] = conn;
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
82
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
83 update_have_clients();
0
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 if last_chunk then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 conn:write(last_chunk);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 conn:setlistener(listener);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 return true;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93
6
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
94 local function mark_active(request)
13
c245afa537bb clients.lua: Fix for mark_active()
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
95 local cookie = request.headers.cookie;
6
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
96 if not cookie then
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
97 log("warn", "Active client with no cookie");
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
98 return;
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
99 end
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
100 local conn = client_by_cookie[cookie];
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
101 if not conn then
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
102 log("warn", "Active client with no connection");
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
103 return;
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
104 end
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
105 active_clients[conn] = os.time();
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
106 update_have_clients();
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
107 end
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
108
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
109 function handle_active(event, path)
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
110 mark_active(event.request);
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
111 return "OK";
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
112 end
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
113
7
59655d6c45b3 client.lua: Add /watchers endpoint to get the current number of people watching the feed
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
114 function handle_watchers(event)
59655d6c45b3 client.lua: Add /watchers endpoint to get the current number of people watching the feed
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
115 local active, total = 0, 0;
59655d6c45b3 client.lua: Add /watchers endpoint to get the current number of people watching the feed
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
116 for client in pairs(clients) do
59655d6c45b3 client.lua: Add /watchers endpoint to get the current number of people watching the feed
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
117 total = total + 1;
59655d6c45b3 client.lua: Add /watchers endpoint to get the current number of people watching the feed
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
118 if active_clients[client] then
59655d6c45b3 client.lua: Add /watchers endpoint to get the current number of people watching the feed
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
119 active = active + 1;
59655d6c45b3 client.lua: Add /watchers endpoint to get the current number of people watching the feed
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
120 end
59655d6c45b3 client.lua: Add /watchers endpoint to get the current number of people watching the feed
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
121 end
59655d6c45b3 client.lua: Add /watchers endpoint to get the current number of people watching the feed
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
122 mark_active(event.request);
59655d6c45b3 client.lua: Add /watchers endpoint to get the current number of people watching the feed
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
123 return tostring(total);
59655d6c45b3 client.lua: Add /watchers endpoint to get the current number of people watching the feed
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
124 end
59655d6c45b3 client.lua: Add /watchers endpoint to get the current number of people watching the feed
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
125
8
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
126 -- Called when a HTTP stream client closes
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
127 local function client_closed(request)
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
128 local stream = request._watching;
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
129 stream:remove_watcher(request.conn);
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
130 end
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
131
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
132 function handle_notifications(event)
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
133 event.response._watching = data_stream;
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
134 event.response.on_destroy = client_closed;
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
135 data_stream:add_watcher(event.response.conn);
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
136 return true;
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
137 end
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
138
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
139 function handle_push(event, path)
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
140 local push_event = event.request.path:match("[^/]+$");
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
141 if not push_event then
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
142 log("debug", "Error parsing push path: %s", tostring(event.request.path));
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
143 return;
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
144 end
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
145 log("debug", "Pushing notification for %s", tostring(push_event));
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
146 local params = formdecode(event.request.url.query or "");
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
147 local data;
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
148 if type(params) == "table" then
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
149 data = {};
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
150 for _, e in ipairs(params) do
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
151 data[e.name] = e.value;
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
152 end
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
153 end
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
154 data_stream:push(json.encode({ event = push_event, data = data }));
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
155 return 200;
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
156 end
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
157
0
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 events.add_handler("image-changed", function (event)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 log("debug", "New image");
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160 local chunk_data = table.concat({
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161 "--BoundaryString",
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162 "Content-Type: image/jpeg";
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163 "Content-Length: "..#event.image;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164 "";
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
165 event.image;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166 }, "\r\n");
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167 last_chunk = ("%x\r\n%s\r\n"):format(#chunk_data, chunk_data);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168
6
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
169 local time_now = os.time();
0
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170 for client in pairs(clients) do
6
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
171 local active_time = active_clients[client];
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
172 if active_time and time_now - active_time < activity_timeout then
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
173 client:write(last_chunk);
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
174 else
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
175 active_clients[client] = nil;
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
176 end
0
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
177 end
6
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
178 update_have_clients();
0
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179 end);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
180
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
181 http_server.add_host("localhost");
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
182 http_server.set_default_host("localhost");
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
183 http_server.add_handler("GET localhost/*", handle_request);
6
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
184 http_server.add_handler("GET localhost/active", handle_active);
7
59655d6c45b3 client.lua: Add /watchers endpoint to get the current number of people watching the feed
Matthew Wild <mwild1@gmail.com>
parents: 6
diff changeset
185 http_server.add_handler("GET localhost/watchers", handle_watchers);
8
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
186 http_server.add_handler("GET localhost/notifications", handle_notifications);
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
187 http_server.add_handler("GET localhost/push/*", handle_push);
0
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
188 http_server.listen_on(8006);

mercurial