This repository has been archived by the owner on Mar 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.coffee
74 lines (59 loc) · 2.06 KB
/
server.coffee
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
http = require "http"
fs = require "fs"
brow = require "browserver"
engine = require "engine.io"
coffee = require "coffee-script"
router = require "browserver-router"
app = fs.readFileSync "#{__dirname}/app.coffee", "utf8"
assets =
engine: fs.readFileSync "#{__dirname}/node_modules/engine.io-client/dist/engine.io.js"
domo: fs.readFileSync "#{__dirname}/domo.js"
browserver: fs.readFileSync "#{__dirname}/node_modules/browserver/node_modules/browserver-client/browserver.js"
router: fs.readFileSync "#{__dirname}/node_modules/browserver-router/index.js"
app: coffee.compile app
client = Buffer """
<!doctype html>
<html>
<head>
<title>෴ browserver: a node.js HTTP server, in your browser</title>
</head>
<body style="background-color:#eee">
<script>#{assets.engine}</script>
<script>#{assets.domo}</script>
<script>#{assets.browserver}</script>
<script>http.STATUS_CODES = #{JSON.stringify http.STATUS_CODES}</script>
<script>#{assets.router}</script>
<script>#{assets.app}</script>
</body>
</html>
"""
http.globalAgent?.maxSockets = Infinity
httpServer = http.createServer()
httpServer.on "request", router
"/":
GET: (req, res) ->
res.writeHead 200
"Content-Type": "text/html; charset=utf8"
"Content-Length": client.length
res.end client
wsServer = engine.attach httpServer
browServer = new brow.Server
browServer.listen httpServer, hostname: "*.browserver.org"
browServer.listen wsServer
updateCount = ->
serverCount = String Object.keys(browServer.servers).length
for name, server of browServer.servers
req = http.request
method: "PUT"
headers:
host: name
"content-type": "text/plain"
path: "/server-count"
req.end serverCount
browServer.on "connection", updateCount
browServer.on "disconnection", updateCount
httpServer.listen 80, ->
{port, address} = do @address
console.log "now running at http://#{address}:#{port}/"
process.on "uncaughtException", (err) ->
console.error err.message, err.stack