0
"メッセージ"以外のチャンネルを認識できるようにノードjを取得できません。チャネル名「メッセージ」は認識されますが、チャネル名を他の名前に変更すると、ノードはサブスクライブせずにチャネル名に応答します。ノードjsは、別の名前を持つredisチャンネルに登録しません。
例:
var sub = redis.createClient();
sub.subscribe('message');
sub.on('message', function (channel, data) {
data = JSON.parse(data);
console.log('chat', channel, data);
});
/*
* Server
*/
// Start listening for incoming client connections
io.sockets.on('connection', function (socket) {
console.log('NEW CLIENT CONNECTED');
socket.on('disconnect', function() {
console.log('DISCONNECT');
//I've tried using unsubscribe to message to fix problem
//sub.unsubscribe('message');
});
});
コンソールに上記のコードが動作して出力:
:chat message { room: 1 }
DISCONNECT
NEW CLIENT CONNECTED
以下のコードは、チャネルサブスクリプションログを記録しません
NEWのCLIENT CONNECTED DISCONNECT
var sub = redis.createClient();
sub.subscribe('chat');
//Does not output anything to console
sub.on('chat', function (channel, data) {
data = JSON.parse(data);
console.log('chat', channel, data);
});
/*
* Server
*/
// Start listening for incoming client connections
io.sockets.on('connection', function (socket) {
console.log('NEW CLIENT CONNECTED');
socket.on('disconnect', function() {
console.log('DISCONNECT');
//I've tried using unsubscribe to message to fix problem
//sub.unsubscribe('message');
});
});
のRedis-CLI MONITOR:
1485546938.714486 [0 127.0.0.1:49881] "info"
1485546938.717446 [0 127.0.0.1:49881] "subscribe" "message"
1485546945.902959 [0 127.0.0.1:49897] "SELECT" "0"
1485546945.903406 [0 127.0.0.1:49897] "PUBLISH" "message" "{\"room\":1}"
1485546868.850918 [0 127.0.0.1:49857] "info"
1485546868.853824 [0 127.0.0.1:49857] "subscribe" "chat"
1485546875.332391 [0 127.0.0.1:49873] "SELECT" "0"
1485546875.332861 [0 127.0.0.1:49873] "PUBLISH" "chat" "{\"room\":1}"
「私は非常に感謝します。 – user3325126
@ user3325126私はEventEmitterを読んでいます:https://nodejs.org/api/events.html#events_emitter_on_eventname_listener – Josh