# HG changeset patch # User Waqas Hussain # Date 1301695804 -18000 # Node ID 98569ec25ac28c4ff0c332936f2b3cea17db4101 # Parent ee99eafdd1685d6c7014073d11932d7f67e37700 mod_json_streams: Add BOSH support (on HTTP path "/jsonstreams"). diff -r ee99eafdd168 -r 98569ec25ac2 mod_json_streams/mod_json_streams.lua --- a/mod_json_streams/mod_json_streams.lua Sat Apr 02 00:04:26 2011 +0500 +++ b/mod_json_streams/mod_json_streams.lua Sat Apr 02 03:10:04 2011 +0500 @@ -4,6 +4,7 @@ module.host = "*" +local httpserver = require "net.httpserver"; local filters = require "util.filters" local json = require "util.json" @@ -120,4 +121,34 @@ filters.remove_filter_hook(filter_hook); end +function encode(data) + if type(data) == "string" then + data = json.encode({ s = data }); + elseif type(data) == "table" and data.body then + data.body = json.encode({ s = data.body }); + data.headers["Content-Type"] = "application/json"; + end + return data; +end +function handle_request(method, body, request) + local mod_bosh = modulemanager.get_module("*", "bosh") + if mod_bosh then + if body and method == "POST" then + pcall(function() body = json.decode(body).s; end); + end + local _send = request.send; + function request:send(data) return _send(self, encode(data)); end + return encode(mod_bosh.handle_request(method, body, request)); + end + return "mod_bosh not loaded"; +end +local function setup() + local ports = module:get_option("jsonstreams_ports") or { 5280 }; + httpserver.new_from_config(ports, handle_request, { base = "jsonstreams" }); +end +if prosody.start_time then -- already started + setup(); +else + prosody.events.add_handler("server-started", setup); +end