Advertisement
Guest User

Untitled

a guest
Oct 17th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2. const fs = require('fs'), net = require('net'),filename = process.argv[2];
  3.  
  4.  
  5. let clients = [];
  6. let server = net.createServer( (connection) => {
  7.     console.log('Subscriber connected.');
  8.     clients.push(connection);
  9.  
  10.     connection.on('close', () => {
  11.         console.log('Subscriber disconnected');
  12.         clients.pop(connection);
  13.     })
  14. });
  15.  
  16.  
  17. let watcher = fs.watch(filename, function(eventType, filename) {
  18.     if(eventType === "change"){
  19.         clients.forEach(function(element) {
  20.             element.write(JSON.stringify({type: 'changed', file: filename,
  21.             timestamp: Date.now() }) + '\n');
  22.         });
  23.         }else if(eventType === "rename"){
  24.             clients.forEach(function(element) {
  25.                 element.write(JSON.stringify({type: 'nameChanged', file: filename}) + '\n');
  26.             });
  27.         }
  28. });
  29.  
  30.  
  31. //if (!filename) { throw Error('No target filename was specified.'); }
  32.  
  33. server.listen(5433, function() {
  34.   console.log('Listening for subscribers...');
  35. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement