有一些方法可以使用Node JS流式传输照片

There is some way to stream a photo with Node JS?

本文关键字:JS 传输 照片 Node 方法 可以使      更新时间:2023-09-26

我正在创建一个网络聊天,我想为用户提供从其文件系统中选择图像并通过流式传输将其共享给聊天室的其他成员的选项。

有办法做到这一点吗?

谢谢!

将照片流式传输到服务器,然后流式传输到房间的连接客户端,对于服务器来说不会有这样的开销,因为它使用流,可以以不同的方式处理,压缩,恢复,暂停等。

Socket.io可能不是传输二进制数据的>>最佳<<解决方案,但您始终可以对图像进行 base64 编码并将字符串用于所有目的。

当然,correct解决方案将在不久的将来与WebRTC和诸如PeerJS.

希望它有帮助

看看 BinaryJS

// Incoming stream from browsers
  client.on('stream', function(stream, meta){
    // broadcast to all other clients
    for(var id in bs.clients){
      if(bs.clients.hasOwnProperty(id)){
        var otherClient = bs.clients[id];
        if(otherClient != client){
          var send = otherClient.createStream(meta); // create writable stream
          stream.pipe(send); //pipe data to otherClient
        }
      }
    }
  });