2016-06-10 65 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?你有一个适合我的代码的简单例子吗?谢谢。 –

相关问题