Guest User

xcv

a guest
Aug 4th, 2014
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var fs = require('fs');
  2. var fileName = "test.csv";
  3. var fileToArray = [];
  4. var fileToJson;
  5. fs.exists(fileName, function(exists) {
  6.   if (exists) {
  7.     fs.stat(fileName, function(error, stats) {
  8.       fs.open(fileName, "r", function(error, fd) {
  9.         var buffer = new Buffer(stats.size);
  10.         fs.read(fd, buffer, 0, buffer.length, null, function(error, bytesRead, buffer) {
  11.           var data = buffer.toString("utf8", 0, buffer.length);
  12.           var lines = data.split("\r\n");
  13.           var fileColCount = 6;
  14.           var lineInArray = [];
  15.           lines.forEach(function(line) {
  16.             lineInArray = line.split(',');
  17.             lineInArray.pop();
  18.             fileToArray.push(lineInArray);
  19.           })
  20.           console.log(fileToArray);
  21.           console.log('***********');
  22.           fileToJson = JSON.stringify(fileToArray);
  23.           console.log(fileToJson);
  24.           fs.close(fd);
  25.         });
  26.       });
  27.     });
  28.   }
  29. });
  30.  
  31. var socketio = require('socket.io');
  32. var io = socketio.listen(3636);
  33.  
  34.  
  35. if (fileToJson) { // Empty?
  36.   console.log(fileToJson);
  37. } else {
  38.   console.log('fileToJson empty !!!');
  39. }
  40. io.sockets.on('connection', function(socket) {
  41.   var clientIp = socket.request.connection.remoteAddress
  42.   console.log('Connection ip address: ' + clientIp);
  43.  
  44.   socket.emit('candles', {
  45.     'jsoncandles': fileToJson
  46.   });
  47.  
  48.   //send data to client
  49.   setInterval(function() {
  50.     socket.emit('date', {
  51.       'date': new Date()
  52.     });
  53.   }, 1000);
  54.  
  55.   //recieve client data
  56.   socket.on('client_data', function(data) {
  57.     process.stdout.write(data.letter);
  58.   });
  59.  
  60. });
Advertisement
Add Comment
Please, Sign In to add comment