Node.js BinaryServer:发送消息到客户端在stream结束?

我正在使用node.js BinaryServerstream二进制数据,我想从客户端调用.Stream.end()函数后,从服务器的callback事件。

我似乎无法理解 – 当node.js服务器实际closuresstream连接时,如何发送消息或某种通知?

节点JS:

 server.on('connection', function(client) { client.on('stream', function (stream, meta) { stream.on('end', function () { fileWriter.end(); // <--- I want to send an event to the client here }); }); }); 

客户端JS:

 client = new BinaryClient(nodeURL); window.Stream = client.createStream({ metaData }); .... window.Stream.end(); // <--- I want to recieve the callback message 

在服务器端,您可以使用.send将stream发送到客户端。 您可以发送各种数据types,但在这种情况下,一个简单的string可能就足够了。

在客户端,您还可以收听'stream'事件以从服务器接收数据。

节点JS:

 server.on('connection', function(client) { client.on('stream', function (stream, meta) { stream.on('end', function () { fileWriter.end(); client.send('finished'); }); }); }); 

客户端JS:

 client = new BinaryClient(nodeURL); client.on('stream', data => { console.log(data); // do something with data }); window.Stream = client.createStream({ metaData }); .... window.Stream.end();