Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 节点js赢得';我不能用另一个名字订阅redis频道_Node.js_Redis_Socket.io - Fatal编程技术网

Node.js 节点js赢得';我不能用另一个名字订阅redis频道

Node.js 节点js赢得';我不能用另一个名字订阅redis频道,node.js,redis,socket.io,Node.js,Redis,Socket.io,我无法让node js识别除“消息”之外的任何其他通道。可以识别通道名称“message”,但当我将通道名称更改为任何其他名称时,节点不会订阅和响应通道名称 示例: var sub = redis.createClient(); sub.subscribe('message'); sub.on('message', function (channel, data) { data = JSON.parse(data); console.log('chat', channel, d

我无法让node js识别除“消息”之外的任何其他通道。可以识别通道名称“message”,但当我将通道名称更改为任何其他名称时,节点不会订阅和响应通道名称

示例:

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
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}"
上述代码工作并输出到控制台:

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
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}"
以下代码不记录频道订阅日志:

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
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}"
新客户端已连接 断开

redis cli监视器:

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
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}"

无论通道名称如何,参数始终为“消息”。它是这样的:

sub.on("message", function (channel, message) {
  console.log("sub channel " + channel + ": " + message);
});

无论通道名称如何,参数始终为“消息”。它是这样的:

sub.on("message", function (channel, message) {
  console.log("sub channel " + channel + ": " + message);
});

如果您能为我提供关于“on”方法的文档,我将非常感谢。@user3325126我将阅读EventEmitter:如果您能为我提供关于“on”方法的文档,我将非常感谢。@user3325126我将阅读EventEmitter: