有什么方法可以通过 socket.io 将图像发送给客户端?

is there any way to send image to client by socket.io?

我想制作一个可以实时发送图像的网络应用程序。有什么办法吗? 任何视频 link 或文档 link 都会很有帮助。

谢谢

正如the official website所说,socket.io可以处理blob对象。

Starting in 1.0, it's possible to send any blob back and forth: image, audio, video.

例如,您可以从 socket.io 服务器端推送文档,如下所示。从client端推送到server端跟这个差不多

服务器端

const filePath = "./img.jpg"
const imgBinary = fs.readFileSync(filePath)
socket.emit("img",imgBinary)

客户端

socket.on("img", imgBinary => {
    const blob = new Blob([imgBinary])
    const imgBlobUrl = window.URL.createObjectURL(blob)
    // Insert imgBlobUrl into img.src or something
});