2016-06-10 79 views
1

我的代碼顯示一個簡單的上傳表單(node-formidablenode.js)。我正在使用socket.io來更新客戶端上的當前上傳文件進度。 我的問題是我目前正在向每個客戶端發送進度更新。作爲一個例子,如果我用clientA開始上傳,然後用clientB連接到網站,clientB將看到與clientA完全相同的進度條。通常,clientA和clientB應該不同,它們各自的進度條僅與其各自的上載鏈接。使用node.js,node-formidable和socket.io向客戶端發送上傳進度更新

這是我的app.js文件

// Required modules 
var formidable = require('formidable'), 
    http = require('http'), 
    util = require('util'), 
    fs = require('fs-extra'); 

// Path to save file, server side 
var savePath = "./uploadedFiles/"; 

// Loading index.html 
var server = http.createServer(function(req, res) { 
    // Form uploading Process code 
    //Upload route 
    if (req.url == '/upload' && req.method.toLowerCase() == 'post') { 
     // creates a new incoming form. 
     var form = new formidable.IncomingForm(); 

     // set the path to save the uploaded file on server 
     form.uploadDir = savePath; 

     // updating the progress of the upload 
     form.on('progress', function(bytesReceived, bytesExpected) { 
      io.sockets.in('sessionId').emit('uploadProgress', (bytesReceived * 100)/bytesExpected); 
     }); 

     // parse a file upload 
     form.parse(req, function (err, fields, files) { 
      res.writeHead(200, { 'content-type': 'text/plain' }); 
      res.write('Upload received :\n'); 
      res.end(util.inspect({ fields: fields, files: files })); 
     }); 
     form.on('end', function (fields, files) { 
      /* Temporary location of our uploaded file */ 
      var temp_path = this.openedFiles[0].path; 
      /* The file name of the uploaded file */ 
      var file_name = this.openedFiles[0].name; 
      /* Files are nammed correctly */ 
      fs.rename(temp_path, savePath + file_name, function(err) { 
       if (err) console.log('ERROR: ' + err); 
      }); 
     }); 
     return; 
    } 

    fs.readFile('./index.html', 'utf-8', function(error, content) { 
     res.writeHead(200, {"Content-Type": "text/html"}); 
     res.end(content); 
    }); 
}); 

// socket.io 
var io = require('socket.io').listen(server); 

io.on('connection', function (socket) { 
    socket.join("sessionId"); 
}); 

server.listen(8080); 

這是我的index.html文件

<!DOCTYPE html> 
<html> 
    <head> 
     <meta charset="utf-8" /> 
     <title>Socket.io</title> 
    </head> 

    <body> 
     <h1>Test d'upload</h1> 

     <script src="/socket.io/socket.io.js"></script> 
     <script> 
      var socket = io.connect('http://localhost:8080'); 
      socket.on('uploadProgress' , function (progress){ 
       document.getElementById("currentProgress").value = progress; 
      }); 
     </script> 

     <form action="/upload" method="post" enctype="multipart/form-data"> 
     <input type="file" name="upload" multiple="multiple"><br> 
     <input type="submit" value="Upload"> 
     </form> 

     <p><progress id="currentProgress" value="0" max="100"></progress></p> 
    </body> 
</html> 

我沒有比這更多的代碼。我對node.js和socket.io很陌生,所以我不確定它們之間以及客戶端和服務器之間的交互。

如何更改我的代碼以僅更新正確的客戶端?

無論如何謝謝你的時間。

回答

1

一個建議 有一個基本的方式

socketIO.sockets.socket(this.socketid).emit(,dataObj);

如果你的函數是你的套接字代碼裏面...否則我會經常做這樣的事情

connection.query(「選擇websocket_sessions其中USER_ID = socketid」 + data.form.user_id,功能(ERR ,用戶)socketIO.sockets.socket(users [0] .socketid).emit(,dataObj); });

其中我總是更新或

+0

我試圖在我提供的代碼中使用它,它不起作用。你如何獲得用戶ID?你有一個適合我的代碼的簡單例子嗎?謝謝。 –

相關問題