-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
56 lines (43 loc) · 1.44 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
50
51
52
53
54
55
56
var express = require('express')
var SSE = require('sse')
var EventEmitter = require('events').EventEmitter
var uuid = require('node-uuid')
var bodyParser = require('body-parser')
var app = express()
var board_exchange = new EventEmitter()
board_exchange.setMaxListeners(1024)
app.set('view engine', 'jade')
app.use('/public', express.static('public'))
app.use(bodyParser.json()) // for parsing application/json
app.use(bodyParser.text()) // for parsing application/json
app.get('/', function (req, res) {
res.render('index', { hash: uuid.v4() })
})
app.get('/board/:id', function (req, res) {
console.log(`client (${req.params.id}) connected`)
var client = new SSE.Client(req, res)
client.initialize()
client.send('oh', 'hi')
board_exchange.on(req.params.id, function (board) {
client.send('board', JSON.stringify(board))
})
var interval = setInterval(function () {
client.send('heartbeat', '<3')
}, 1000)
client.on('close', function () {
console.log(`client (${req.params.id}) gone`)
clearInterval(interval)
})
})
app.get('/:id', function (req, res) {
res.render('board', { id: req.params.id })
})
app.post('/:id', function (req, res) {
board_exchange.emit(req.params.id, req.body)
res.end('LONG LIFE CONWAY')
})
var server = app.listen(process.env.PORT || 3000, function () {
var host = server.address().address
var port = server.address().port
console.log('Example app listening at http://%s:%s', host, port)
})