Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- const fs = require('fs'), net = require('net'),filename = process.argv[2];
- let clients = [];
- let server = net.createServer( (connection) => {
- console.log('Subscriber connected.');
- clients.push(connection);
- connection.on('close', () => {
- console.log('Subscriber disconnected');
- clients.pop(connection);
- })
- });
- let watcher = fs.watch(filename, function(eventType, filename) {
- if(eventType === "change"){
- clients.forEach(function(element) {
- element.write(JSON.stringify({type: 'changed', file: filename,
- timestamp: Date.now() }) + '\n');
- });
- }else if(eventType === "rename"){
- clients.forEach(function(element) {
- element.write(JSON.stringify({type: 'nameChanged', file: filename}) + '\n');
- });
- }
- });
- //if (!filename) { throw Error('No target filename was specified.'); }
- server.listen(5433, function() {
- console.log('Listening for subscribers...');
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement