clients.lua

Mon, 04 Jan 2016 16:27:00 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Mon, 04 Jan 2016 16:27:00 +0000
changeset 16
d35376a53644
parent 15
67858d731518
child 17
dd2a570367a9
permissions
-rw-r--r--

main, geoip: Add GeoIP lookup support for watcher info

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
14
1d28dfcd9c94 clients.lua: Bump inactivity timeout to 70s
Matthew Wild <mwild1@gmail.com>
parents: 13
diff changeset
39 local activity_timeout = 70;
6
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
15
67858d731518 clients.lua: Add /watcher_info endpoint to get a more detailed list of streams
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
85 events.fire_event("new-client", { conn = conn, cookie = cookie });
67858d731518 clients.lua: Add /watcher_info endpoint to get a more detailed list of streams
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
86
0
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 if last_chunk then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 conn:write(last_chunk);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 end
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 conn:setlistener(listener);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 return true;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95
6
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
96 local function mark_active(request)
13
c245afa537bb clients.lua: Fix for mark_active()
Matthew Wild <mwild1@gmail.com>
parents: 8
diff changeset
97 local cookie = request.headers.cookie;
6
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
98 if not cookie then
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
99 log("warn", "Active client with no cookie");
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
100 return;
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
101 end
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
102 local conn = client_by_cookie[cookie];
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
103 if not conn then
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
104 log("warn", "Active client with no connection");
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
105 return;
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
106 end
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
107 active_clients[conn] = os.time();
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
108 update_have_clients();
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
109 end
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
110
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
111 function handle_active(event, path)
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
112 mark_active(event.request);
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
113 return "OK";
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
114 end
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
115
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
116 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
117 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
118 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
119 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
120 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
121 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
122 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
123 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
124 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
125 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
126 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
127
15
67858d731518 clients.lua: Add /watcher_info endpoint to get a more detailed list of streams
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
128 function handle_watcher_info(event)
67858d731518 clients.lua: Add /watcher_info endpoint to get a more detailed list of streams
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
129 local watchers = {};
67858d731518 clients.lua: Add /watcher_info endpoint to get a more detailed list of streams
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
130 for client in pairs(clients) do
67858d731518 clients.lua: Add /watcher_info endpoint to get a more detailed list of streams
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
131 local bytes_rx, bytes_tx, age_sec = client:socket():getstats();
67858d731518 clients.lua: Add /watcher_info endpoint to get a more detailed list of streams
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
132 table.insert(watchers, json.encode{
67858d731518 clients.lua: Add /watcher_info endpoint to get a more detailed list of streams
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
133 location = client.location or "(Unknown location)";
67858d731518 clients.lua: Add /watcher_info endpoint to get a more detailed list of streams
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
134 active = not not active_clients[client];
67858d731518 clients.lua: Add /watcher_info endpoint to get a more detailed list of streams
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
135 age = math.floor(age_sec);
67858d731518 clients.lua: Add /watcher_info endpoint to get a more detailed list of streams
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
136 sent = math.floor(bytes_tx/1024);
67858d731518 clients.lua: Add /watcher_info endpoint to get a more detailed list of streams
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
137 });
67858d731518 clients.lua: Add /watcher_info endpoint to get a more detailed list of streams
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
138 end
67858d731518 clients.lua: Add /watcher_info endpoint to get a more detailed list of streams
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
139 mark_active(event.request);
67858d731518 clients.lua: Add /watcher_info endpoint to get a more detailed list of streams
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
140 return "[\n "..table.concat(watchers, ",\n ").."\n]";
67858d731518 clients.lua: Add /watcher_info endpoint to get a more detailed list of streams
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
141 end
67858d731518 clients.lua: Add /watcher_info endpoint to get a more detailed list of streams
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
142
8
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
143 -- Called when a HTTP stream client closes
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
144 local function client_closed(request)
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
145 local stream = request._watching;
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
146 stream:remove_watcher(request.conn);
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
147 end
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
148
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
149 function handle_notifications(event)
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
150 event.response._watching = data_stream;
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
151 event.response.on_destroy = client_closed;
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
152 data_stream:add_watcher(event.response.conn);
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
153 return true;
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
154 end
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
155
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
156 function handle_push(event, path)
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
157 local push_event = event.request.path:match("[^/]+$");
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
158 if not push_event then
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
159 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
160 return;
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
161 end
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
162 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
163 local params = formdecode(event.request.url.query or "");
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
164 local data;
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
165 if type(params) == "table" then
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
166 data = {};
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
167 for _, e in ipairs(params) do
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
168 data[e.name] = e.value;
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
169 end
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
170 end
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
171 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
172 return 200;
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
173 end
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
174
0
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
175 events.add_handler("image-changed", function (event)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
176 log("debug", "New image");
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
177 local chunk_data = table.concat({
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
178 "--BoundaryString",
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179 "Content-Type: image/jpeg";
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
180 "Content-Length: "..#event.image;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
181 "";
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
182 event.image;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
183 }, "\r\n");
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
184 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
185
6
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
186 local time_now = os.time();
0
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
187 for client in pairs(clients) do
6
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
188 local active_time = active_clients[client];
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
189 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
190 client:write(last_chunk);
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
191 else
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
192 active_clients[client] = nil;
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
193 end
0
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
194 end
6
4e46ef3035ba clients.lua: Add client activity tracking
Matthew Wild <mwild1@gmail.com>
parents: 0
diff changeset
195 update_have_clients();
0
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
196 end);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
197
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
198 http_server.add_host("localhost");
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
199 http_server.set_default_host("localhost");
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
200 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
201 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
202 http_server.add_handler("GET localhost/watchers", handle_watchers);
15
67858d731518 clients.lua: Add /watcher_info endpoint to get a more detailed list of streams
Matthew Wild <mwild1@gmail.com>
parents: 14
diff changeset
203 http_server.add_handler("GET localhost/watcher_info", handle_watcher_info);
8
2e4c32c4fb6b clients.lua: Add realtime push support
Matthew Wild <mwild1@gmail.com>
parents: 7
diff changeset
204 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
205 http_server.add_handler("GET localhost/push/*", handle_push);
0
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
206 http_server.listen_on(8006);

mercurial