简体   繁体   English

socket.io-stream发射与打开

[英]socket.io-stream emit vs on

The socket.io-stream documentation contains the following example for sending and receiving streaming data: socket.io-stream文档包含以下用于发送和接收流数据的示例:

// send data
ss(socket).on('file', function(stream) {
  fs.createReadStream('/path/to/file').pipe(stream);
});

// receive data
ss(socket).emit('file', stream);
stream.pipe(fs.createWriteStream('file.txt'));

According to this example, it seems that we should use "on" to send data and "emit" to receive data. 根据此示例,似乎我们应该使用“ on”发送数据并使用“ emit”接收数据。 But this seems contrary to all the examples I've seen so far - that we should use "emit" to send data and "on" to receive it. 但这似乎与我到目前为止所看到的所有示例相反-我们应该使用“发射”来发送数据,而应该使用“打开”来接收数据。 Which is correct? 哪个是对的? And how should I listen for streaming events? 以及我该如何收听流事件?

emit will send a message and on will listen to the message. emit将发送一条消息,然后继续收听该消息。 Now depending on where you are in the server or the client the message will go to the other side ie if you're on the client and you emit then the server will receive and vice versa. 现在,取决于您在服务器或客户端中的位置,消息将转到另一侧,即,如果您在客户端中并且发出,则服务器将接收消息,反之亦然。 What you're doing here is using the npm package socket.io-stream which augments the socket to stream data. 您在这里使用的是npm软件包socket.io-stream ,它扩展了套接字以流式传输数据。 The idea is the client requests the image and the server "streams" it to them. 这个想法是客户端请求图像,然后服务器将其“流化”到它们。

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

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