所以,我被困在我的 IoT 项目中,我必须在拍摄后立即在我的网页上实时显示用我的 picam 拍摄的照片(托管在 000webhost 上)。 p>
这是我尝试过的: 在我的 raspberry pi 上,我有一个 python 代码将图像转换为 Base64(使用 pubnub 发布)并使用 Pubnub javascript sdk 接收 base64 字符串并使用它解码并显示图像。 问题是: Pubnub 一次只允许发送近 35kb 的数据
有没有一种方法可以让我在后端用我的 picam 拍照并通过显示当时拍摄的照片来更新我的网页。
(我正在使用 pubnub 从我的网页发送消息(触发器)到我树莓派上的 python 脚本来拍照)
最佳答案
ℹ️ 快速提示:PubNub 在发布时接受 GZIP 内容
This is not a direct answer. It's just a good tip!
当您 gzip POST your publish calls 时,您可以发布更大的图像尺寸.您可以轻松发布多种语言的 gzip 压缩数据。这是 bash 中的一个简单示例。下面还有一个 nodejs 的。
#!/bin/bash
## -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
## PubNub POST GZIP Example
## -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
echo '{"hello":["world",123,null]}' | gzip -c9 | \
curl \
-H "Content-Encoding: gzip" \
--data-binary @- \
'http://pubsub.pubnub.com/publish/demo/demo/0/my_channel/0'
echo
使用 NodeJS 发布 GZIP 数据
您也可以使用以下示例在 Node.JS 中执行此操作。
var http = require("http");
var zlib = require("zlib");
exports.publish = function(msg) {
var req = http.request({
"host" : "pubsub.pubnub.com",
"method" : "POST",
"path" : "/publish/demo/demo/0/my_channel/0",
"headers" : {
"Content-Encoding" : "gzip",
"Content-Type" : "application/json; charset=UTF-8",
},
}, function (res) {
var body = "";
res.on("data", function (chunk) {
body += chunk;
});
res.on("end", function () {
console.log(body)
})
});
req.on("error", function (e) {
console.error("ERROR:", e.message);
});
zlib.gzip(msg, function (error, result) {
console.log("data:", result);
req.write(result);
req.end();
});
};
exports.publish('{"hello":["world",123,null]}');
关于javascript - 在网页(不是本地主机)上实时显示图片(位于计算机上),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49625006/