简体   繁体   English

Node.JS单个套接字,多个事件(上传)

[英]Node.JS single socket, multiple events (uploads)

I'm developing an upload tracker (progress bar, etc). 我正在开发一个上传跟踪器(进度栏等)。

So for my front-end I use jQuery File upload. 因此对于我的前端,我使用jQuery File upload。 Back-end upload is done using separate workers and there I use Redis pub/sub. 后端上传是使用单独的工作程序完成的,在那里我使用Redis pub / sub。 So to connect front and back-ends I use Node.JS (socket.io). 因此,要连接前端和后端,我使用Node.JS(socket.io)。

jQuery file upload has a " done " callback function that denotes reaching our file to the server. jQuery文件上传具有一个“ done ”回调函数,该函数表示将我们的文件到达服务器。

This works absolutely fine for a single file. 对于单个文件,这绝对可以正常工作。

Problem arises when I try upload 2+ files simultaneously. 当我尝试同时上传2个以上文件时,会出现问题。 "done" callback function is getting attached to socket listeners 2+ times (correct me if I'm wrong) and as an output I get something like this: “完成”回调函数被附加到套接字侦听器2次以上(如果我错了,请更正我),并且作为输出,我得到这样的东西:

579 " - progress: " 8.333333333333334
580 " - progress: " 8.333333333333334 
579 " - progress: " 16.666666666666668 
580 " - progress: " 16.666666666666668 
579 " - progress: " 25 
580 " - progress: " 25
....
579 " -> upload is finished!" 
580 " -> upload is finished!"

That was an example of uploading 2 files. 那是上传2个文件的示例。 Which doesn't make any sense at all, because they can only be processed one after another. 这根本没有任何意义,因为它们只能一个接一个地处理。

So it should look like this: 所以它应该看起来像这样:

579 " - progress: " 8.333333333333334
579 " - progress: " 16.666666666666668 
579 " - progress: " 25
....

579 " -> upload is finished!"
580 " - progress: " 8.333333333333334
580 " - progress: " 16.666666666666668
....
580 " -> upload is finished!"

Node.js server-side works totally fine. Node.js服务器端工作正常。 This is a log from it: 这是它的日志:

RECEIVED  progress  FROM  upload-579
RECEIVED  progress  FROM  upload-579
RECEIVED  progress  FROM  upload-579
RECEIVED  progress  FROM  upload-579
RECEIVED  progress  FROM  upload-579
RECEIVED  progress  FROM  upload-579
RECEIVED  progress  FROM  upload-579
RECEIVED  progress  FROM  upload-579
RECEIVED  progress  FROM  upload-579
RECEIVED  progress  FROM  upload-579
RECEIVED  progress  FROM  upload-579
RECEIVED  done  FROM  upload-579
RECEIVED  progress  FROM  upload-580
RECEIVED  progress  FROM  upload-580
RECEIVED  progress  FROM  upload-580
RECEIVED  progress  FROM  upload-580
RECEIVED  progress  FROM  upload-580
RECEIVED  progress  FROM  upload-580
RECEIVED  progress  FROM  upload-580
RECEIVED  progress  FROM  upload-580
RECEIVED  progress  FROM  upload-580
RECEIVED  progress  FROM  upload-580
RECEIVED  progress  FROM  upload-580
RECEIVED  done  FROM  upload-580

It received messages from Redis channel and emits appropriate messages to the client side. 它从Redis通道接收消息,并向客户端发出适当的消息。 I think problem lies somewhere on the client side, but I cannot figure out how to modify the code there to get what I want. 我认为问题出在客户端的某个地方,但是我无法弄清楚如何修改那里的代码来获得我想要的东西。 Can something be done to separate them from each other? 可以做些什么使它们彼此分离吗?

PS PS

The socket event callback on the client-side will fire for any emit messages from Node.js. 客户端上的套接字事件回调将触发来自Node.js的任何发射消息。 That's why you are seeing duplicate log messages. 这就是为什么您看到重复的日志消息的原因。 Either send additional data in the socket message, or attach unique events. 在套接字消息中发送其他数据,或附加唯一事件。

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

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