I'm working on a CCTV camera with ESP32-Cam and NodeJS server. The way I want to do it is to use WebSocket client on the ESP and send binary data with captured frames to NodeJS server, which then will forward it to connected users. I can't use socket.io, so I'm using plain websockets (on node 'ws' module).
What I'm struggling about is that while sending binary data I can't tell what kind of message it is, meaning if it is stream, thumbnail, or a plain json message.
我的ESP32 Cam客户端代码(C++):
void liveCam(uint8_t num){
//capture a frame
camera_fb_t * fb = esp_camera_fb_get();
if (!fb) {
Serial.println("Frame buffer could not be acquired");
return;
}
webSocket.sendBIN(num, fb->buf, fb->len);
//return the frame buffer back to be reused
esp_camera_fb_return(fb);
}
以及我在Node中的工作:
this.socket.on("connection", (ws) => {
ws.on("message", (msg) => {
// recognize channel of msg and process it (ArrayBuffer / JSON String)
})
})
要发送JSON,我将使用webSocket.ESP上的sendTXT(使用WebSocketsServer库)方法,只是我不知道how to handle both binary and text messages on the server,以及如何让服务器识别它们(类似于message tag,因此二进制流将是"stream",而json将是"blink"或"fetch\u temp",取决于使用情况).如果没有二进制流,我将只使用json,因此每个消息都有"tag"和"message"属性,但图像流会使事情复杂化.
I had an idea to send a json text message before binary message,这将为发送的下一条消息设置通道.只是不知道它是否可以工作,如果消息不同步,它是否会中断(这可能吗?).此外,我需要一个解决方案as fast as possible(为了保持视频的良好帧速率,现在清晰的二进制流真的很整洁!)
谢谢你的(好) idea 和解决方案!