-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathserver.js
40 lines (32 loc) · 984 Bytes
/
server.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
var watchr = require('watchr')
var compile = require('./lib/compiler')
var server = new (require('ws').Server)({ port: 7777 })
var result
function doCompile() {
compile().then(function(data) {
console.log(new Date() + ' - recompiled >> active connection: ' + connections.length)
result = '/* compiled: ' + new Date() + ' */\n' + data
connections.forEach(function(connection) {
connection.send(result)
})
})
.otherwise(console.error)
}
watchr.watch({
path: 'src',
preferredMethods: ['watchFile','watch'],
listener: function() {
doCompile()
}
})
doCompile()
var connections = [ ]
server.on('connection', function(connection) {
connections.push(connection)
connection.send(result)
console.log('connected >> active connections: ' + connections.length)
connection.on('close', function() {
connections.splice(connections.indexOf(connection), 1)
console.log('disconnected >> active connections: ' + connections.length)
})
})