-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
49 lines (40 loc) · 1.18 KB
/
index.js
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
var http = require("http")
, boot = require("boot")
, MultiChannel = require("multi-channel-mdm")
, browserify = require("browserify")
, ecstatic = require("ecstatic")(__dirname + "/static")
, Router = require("routes").Router
var httpRouter = new Router()
httpRouter.addRoute("/", ecstatic)
httpRouter.addRoute("/bundle.js", bundleBrowserify)
var server = http.createServer(httpHandler)
server.listen(8080)
console.log("listening on port", 8080)
var streamRouter = new Router()
streamRouter.addRoute("/room/:streamName", MultiChannel())
var sock = boot(streamHandler)
sock.install(server, '/boot')
console.log("sock hooked on", "/boot")
function streamHandler(stream) {
var route = streamRouter.match(stream.meta)
if (route) {
route.fn(stream, route.params)
}
}
function httpHandler(req, res) {
var route = httpRouter.match(req.url)
if (route) {
route.fn(req, res)
}
}
function bundleBrowserify(req, res) {
var b = browserify()
b.addEntry("browser.js")
res.setHeader("content-type", "application/jsonn")
try {
res.end(b.bundle())
} catch (err) {
res.statusCode = 500
res.end(err.message)
}
}