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

时间:2014-09-19 06:54:18

标签: javascript node.js callback socket.io jquery-file-upload

我正在开发一个上传跟踪器(进度条等)。

所以对于我的前端我使用jQuery文件上传。 后端上传是使用单独的工作人员完成的,我使用Redis pub / sub。 所以要连接前端和后端,我使用Node.JS(socket.io)。

jQuery文件上传有一个" done"表示将文件传送到服务器的回调函数。

这对于单个文件非常有效。

当我尝试同时上传2个以上的文件时出现问题。 "完成"回调函数被连接到套接字监听器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!"

这是上传2个文件的示例。这根本没有任何意义,因为它们只能一个接一个地处理。

所以看起来应该是这样的:

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服务器端工作正常。 这是一个日志:

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

它收到来自Redis频道的消息,并向客户端发出适当的消息。 我认为问题存在于客户端,但我无法弄清楚如何修改代码以获得我想要的东西。 可以做些什么来将它们彼此分开吗?

P.S

1 个答案:

答案 0 :(得分:1)

客户端的套接字事件回调将触发来自Node.js的任何发出消息。这就是您看到重复日志消息的原因。在套接字消息中发送附加数据,或附加唯一事件。