简体   繁体   English

使用connect-form,socket.io和express在node.js上的行为确实很奇怪

[英]really strange behaviour on node.js using connect-form, socket.io and express

the following code: 以下代码:

req.form.on('progress', function(bytesReceived, bytesExpected){
    var percent = (bytesReceived / bytesExpected * 100) | 0;
    // progressEvent.download(percent);

    io.sockets.on('connection', function (socket) {        
      socket.emit('progress', { percent: percent});
      client = socket;
      });        
  });

written on an http post handler (express.js) sends socket messages to the client js, but it obviously creates a huge amount of listeners, in fact it warns me saying: "node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit." 写在http post处理程序(express.js)上的套接字会向客户端js发送套接字消息,但显然会创建大量的侦听器,实际上它警告我说:“节点”警告:可能检测到EventEmitter内存泄漏。已添加11个侦听器。使用generator.setMaxListeners()增加限制。”

on the other hand this code: 另一方面,此代码:

io.sockets.on('connection', function (socket) {
    progressEvent.on('progress', function(percentage) {
    console.log(percentage);
    socket.emit('progress', { percent: percentage});
    });
});

Doesn't send any message back to the client, the ProgressEvent is: 不将任何消息发送回客户端,ProgressEvent为:

var util = require('util'),
    events = require('events');

function ProgressEvent() {
    if(false === (this instanceof ProgressEvent)) {
        return new ProgressEvent();
    }

    events.EventEmitter.call(this);
}

util.inherits(ProgressEvent, events.EventEmitter); util.inherits(ProgressEvent,events.EventEmitter);

ProgressEvent.prototype.download = function(percentage) {
    var self = this;  
    self.emit('progress', percentage);    
}

exports.ProgressEvent = ProgressEvent;

I've been a good day on this strange problem I can't really see why socket.io doesn't send the socket message to the client. 我在这个奇怪的问题上度过了美好的一天,我真的看不到为什么socket.io不会将套接字消息发送给客户端。

the whole project is here: https://github.com/aterreno/superuploader 整个项目在这里: https : //github.com/aterreno/superuploader

Thanks for your attention & help 感谢您的关注和帮助

You shouldn't listen to socket.io connections inside of the progress event. 您不应该在progress事件中监听socket.io连接。 It looks like you're trying to get socket.io to connect when a user uploads a file, but that will not do that. 看起来您在用户上载文件时试图使socket.io连接,但是那样做不会。 Instead it will listen for a new connection each time the progress event fires on the upload, which I'm guessing is pretty often and it's why you're getting the warning about too many listeners. 取而代之的是,每次progress事件在上传文件上触发时,它都会侦听一个新的连接,我猜这很常见,这就是为什么您收到关于过多侦听器的警告的原因。

What you want to do instead is on the client side, when you initialize an upload, tell the server through socket.io. 相反,您想要做的是在客户端,当您初始化上传时,通过socket.io告诉服务器。 Then the server links up that socket.io client with their upload through their session, http://www.danielbaulig.de/socket-ioexpress/ 然后,服务器通过会话http://www.danielbaulig.de/socket-ioexpress/将该socket.io客户端与其上载链接起来

Something like this should do it 这样的事情应该做

io.sockets.on('connection', function(socket) {
  var session = socket.handshake.session;

  socket.on('initUpload', function() {
    session.socket = socket;
  });

  socket.on('disconnect', function() {
    session.socket = null;
  });
});

And then in your route 然后在你的路线上

req.form.on('progress', function(bytesReceived, bytesExpected){
  var percent = (bytesReceived / bytesExpected * 100) | 0;
  if (req.session.socket) {
    socket.emit('progress', percent);
  }
});

This only works with one upload per session, but you get the idea. 这仅适用于每个会话一次上传,但您可以理解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM