1 local socket = require "socket"; |
1 local socket = require "socket"; |
2 local server = require "net.server_select"; |
2 local server = require "net.server_select"; |
3 local http_server = require"net.http.server"; |
3 local http_server = require"net.http.server"; |
4 local new_uuid = require "util.uuid".generate; |
4 local new_uuid = require "util.uuid".generate; |
|
5 local json = require "cjson"; |
|
6 local formdecode = require "util.http".formdecode; |
5 local log = require "util.logger".init("clients"); |
7 local log = require "util.logger".init("clients"); |
6 |
8 |
7 local response_head = table.concat({ |
9 local response_head = table.concat({ |
8 "HTTP/1.1 200 OK"; |
10 "HTTP/1.1 200 OK"; |
9 "Max-Age: 0"; |
11 "Max-Age: 0"; |
117 end |
121 end |
118 mark_active(event.request); |
122 mark_active(event.request); |
119 return tostring(total); |
123 return tostring(total); |
120 end |
124 end |
121 |
125 |
|
126 -- Called when a HTTP stream client closes |
|
127 local function client_closed(request) |
|
128 local stream = request._watching; |
|
129 stream:remove_watcher(request.conn); |
|
130 end |
|
131 |
|
132 function handle_notifications(event) |
|
133 event.response._watching = data_stream; |
|
134 event.response.on_destroy = client_closed; |
|
135 data_stream:add_watcher(event.response.conn); |
|
136 return true; |
|
137 end |
|
138 |
|
139 function handle_push(event, path) |
|
140 local push_event = event.request.path:match("[^/]+$"); |
|
141 if not push_event then |
|
142 log("debug", "Error parsing push path: %s", tostring(event.request.path)); |
|
143 return; |
|
144 end |
|
145 log("debug", "Pushing notification for %s", tostring(push_event)); |
|
146 local params = formdecode(event.request.url.query or ""); |
|
147 local data; |
|
148 if type(params) == "table" then |
|
149 data = {}; |
|
150 for _, e in ipairs(params) do |
|
151 data[e.name] = e.value; |
|
152 end |
|
153 end |
|
154 data_stream:push(json.encode({ event = push_event, data = data })); |
|
155 return 200; |
|
156 end |
|
157 |
122 events.add_handler("image-changed", function (event) |
158 events.add_handler("image-changed", function (event) |
123 log("debug", "New image"); |
159 log("debug", "New image"); |
124 local chunk_data = table.concat({ |
160 local chunk_data = table.concat({ |
125 "--BoundaryString", |
161 "--BoundaryString", |
126 "Content-Type: image/jpeg"; |
162 "Content-Type: image/jpeg"; |
145 http_server.add_host("localhost"); |
181 http_server.add_host("localhost"); |
146 http_server.set_default_host("localhost"); |
182 http_server.set_default_host("localhost"); |
147 http_server.add_handler("GET localhost/*", handle_request); |
183 http_server.add_handler("GET localhost/*", handle_request); |
148 http_server.add_handler("GET localhost/active", handle_active); |
184 http_server.add_handler("GET localhost/active", handle_active); |
149 http_server.add_handler("GET localhost/watchers", handle_watchers); |
185 http_server.add_handler("GET localhost/watchers", handle_watchers); |
|
186 http_server.add_handler("GET localhost/notifications", handle_notifications); |
|
187 http_server.add_handler("GET localhost/push/*", handle_push); |
150 http_server.listen_on(8006); |
188 http_server.listen_on(8006); |