下面是 node.js 中 redis pubsub 的代码。我在读取文件和纯文本后发布二进制数据,有没有办法识别消息上接收的缓冲区是二进制还是文本?
var redis = require("redis"),
pub = redis.createClient(null, null, {
detect_buffers: true
}),
sub = redis.createClient(null, null, {
detect_buffers: true
}),
fs = require("fs");
sub.subscribe("abc");
sub.on("subscribe", function(channel, count) {
fs.readFile("IMG_2693.JPG", function(err, data) {
pub.publish("abc", data);
});
pub.publish("abc", "hello");
});
sub.on("message", function(channel, data) {
// Can I identify if it is binary data or text?
console.dir(data);
});
谢谢!