6
두 개의 클라이언트를 만들려고합니다. 하나는 pub/sub이고, 다른 하나는 표준 연결입니다. 이것은 가능하지 않습니까? 이 작업을 추상화하는 방법이 있어야합니다 :) 기본적으로 test.js를 실행 한 후 get key
을 수행하면 'valueBefore'만 보입니다. 출력 :Redis/Node.js - 쓰기에 문제가있는 클라이언트 2 개 (1 pub/sub)
node test.js
Reply: OK
/Users/franklovecchio/Desktop/development/node/node_modules/redis/index.js:487
throw new Error("Connection in pub/sub mode, only pub/sub commands may
^
Error: Connection in pub/sub mode, only pub/sub commands may be used
at RedisClient.send_command (/Users/franklovecchio/Desktop/development/node/node_modules/redis/index.js:487:15)
at RedisClient.<anonymous> (/Users/franklovecchio/Desktop/development/node/node_modules/redis/index.js:597:27)
at Object._onTimeout (/Users/franklovecchio/Desktop/development/node/distributed-cache/client/test.js:19:12)
at Timer.callback (timers.js:83:39)
코드는 : 당신은 아마 client2
이후 client1
에서 메시지를 게시해야
var redis = require('redis');
var client1 = redis.createClient();
var client2 = redis.createClient();
client2.on('message', function (channel, message) {
console.log('Received a message on channel: ' + channel);
client1.set('key', message, redis.print);
});
client2.subscribe('channel');
client1.set('key', 'valueBefore', redis.print);
setTimeout(
function() {
client2.publish('channel', 'valueAfter');
},3000
);