forked from prophile/srcomp-stream
-
Notifications
You must be signed in to change notification settings - Fork 1
/
http-socket.coffee
59 lines (46 loc) · 1.44 KB
/
http-socket.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
http = require 'http'
server = (comp) ->
svr = http.createServer (request, response) ->
valid = true
valid = false if request.url isnt '/'
if not valid
response.writeHead 404, 'Not Found', 'Content-Type': 'text/plain'
response.end "Not a thing.\n", 'utf-8'
return
allowHeaders = {
'Content-Type': 'text/plain',
'Allow': 'GET, HEAD, OPTIONS',
'Access-Control-Allow-Origin': '*'
}
mainHeaders = {
'Content-Type': 'text/event-stream',
'Access-Control-Allow-Origin': '*'
}
if request.method is 'OPTIONS'
response.writeHead 200, 'OK', allowHeaders
response.end '', 'utf-8'
return
if request.method is 'HEAD'
response.writeHead 200, 'OK', mainHeaders
response.end '', 'utf-8'
return
if request.method isnt 'GET'
response.writeHead 405, 'Method Not Allowed', allowHeaders
response.end '', 'utf-8'
return
response.writeHead 200, 'OK', mainHeaders
sendEvent = (event) ->
try
response.write "event: " + event.event + "\n", 'utf-8'
response.write "data: " + JSON.stringify(event.data) + "\n\n", 'utf-8'
catch error
do stopListening
stopListening = comp.events.onValue sendEvent
for event in comp.seedRecords()
sendEvent event
response.on 'end', ->
do stopListening
# Time out after 25 minutes
svr.timeout = 25 * 60 * 1000
svr
module.exports = server