-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
55 lines (46 loc) · 1.48 KB
/
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Run this server with: node server.js
var filename = "contents.txt",
port = 8080,
wsserv = require('ws').Server,
wss = new wsserv({port: port}),
fs = require('fs'),
ee = require('event-emitter'),
emitter = ee({}), listener;
// Watch the file, emit 'file-changed' event on change.
// Contents might not have changed. Could just be time.
// TODO Check for content difference
var watcher = fs.watch(filename, function (event, filename) {
if (event == 'change') {
emitter.emit('file-changed', filename);
}
});
// Event handler for when the server recieves a connection
wss.on('connection', function connection(ws) {
// Log all incoming messages to the console.
ws.on('message', function incoming(message) {
console.log('received: %s', message);
});
// When a client connects, log to console
ws.on('open', function () {
console.log("New client");
});
// When a client leaves, log to console.
ws.on('close', function () {
console.log("Client left.");
});
// Read the file and send its contents to all clients
var rf = function() {
fs.readFile(filename, 'utf8', function(err, data) {
if (err) throw err;
ws.send(data, function ack(error) {
if (error) console.log(error);
});
});
}
// Send the current contents on a new connection
rf();
// Listens for the 'file-changed' event. Reads file and sends to clients.
emitter.on('file-changed', function(filename) {
rf();
})
});