错误:在订户模式下连接,可能只使用订户命令

时间:2015-10-07 13:12:14

标签: redis publish-subscribe node-redis

我发现在Redis中推送邮件数据时出错。请向我提供一些关于错误的指导。为什么会这样?我想使用nodejs在redis中存储聊天数据。

server.js(server):

/*
 * -------------------
 * Express
 * -------------------
 */
var app = require('express')(),
        session = require("express-session");

app.use(function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Headers", "Content-Type");
    res.header("Access-Control-Allow-Methods", "PUT, GET, POST, DELETE, OPTIONS");
    next();
});
app.set('host', "localhost");
app.set('port', 8080); //process.env.PORT

/*
 * -------------------
 * Socket Connections
 * -------------------
 */
var server = require('http').createServer(app);
var io = require('socket.io')(server);
server.listen(app.get("port"), app.get("host"), function () {
    console.log("Server up and running...");
});
io.set('log level', 3);

/*
 * -------------------
 * Redis 
 * -------------------
 */
var redis = require("redis");
/*
 *  Sender
 */
var publisher = redis.createClient();
/*
 * Receiver
 */
var subscriber = redis.createClient();
/*
 * -------------------
 * Mysql
 * -------------------
 */
/*var mysql = require("mysql");
 var connection = mysql.createConnection({
 host: "localhost",
 user: "root",
 password: "password",
 database: "w"
 });
 connection.connect();*/

/*
 * -------------------
 * functions
 * -------------------
 */
function subscribeMessage(_subscriber) {
    subscriber.subscribe(_subscriber);
    subscriber.on("message", function (channel, message) {
        console.log("redis connection message:" + message);
    });
}
function pushToRedis(data) {
    subscriber.lpush('wchannel', data);
}

/*
 * -------------------
 * Redis Subscriber
 * -------------------
 */
subscriber.subscribe("wchannel");
subscribeMessage("wchannel");
/*subscriber.on("message", function (channel, message) {
 //console.log("client channel recieve from channel : %s, the message : %s", channel, message);
 });*/

/* 
 * -------------------
 * Socket
 * -------------------
 */
io.sockets.on('connection', function (socket) {    
    console.log("app port :: " + app.get("port"));
    socket.on('connection', function (data) {
        console.log('Server running on *:' + app.get('port'));
    });
    //socket.emit("connected", {msg: ""});        
    socket.on("chat", function (data) {
        pushToRedis(data.msg);
        //subscriber.ltrim('messages', 0, 99);
        socket.emit("publishMessage", {msg: data.msg});
    });
    socket.on('disconnect', function () {
    });
});

script.js(client):

$(function () {
    var socket = io.connect("http://localhost:8080/");
    /*
     * -------------------
     * Methods
     * -------------------
     */
    $.fn.clearAndFocus = function () {
        $("#txtMessage").val("").focus();
    };
    $.fn.sendMessageToServer = function () {
        socket.emit("chat", {msg: $("#txtMessage").val()});
        $.fn.clearAndFocus();
    };
    $.fn.clearAndFocus();
    /*
     * -------------------
     * Events
     * -------------------
     */
    $("#btnSend").on("click", function () {
        $.fn.sendMessageToServer();
    });
    $("#txtMessage").on("keyup", function (e) {
        if (e.keyCode === 13) {
            $.fn.sendMessageToServer();
            $.fn.clearAndFocus();
        }
    });
    /*
     * -------------------
     * Socket Events
     * -------------------
     */
    socket.on('connect', function () {
        console.log("w client is connected");
    });
    socket.on("publishMessage", function (data) {
        console.log(data.msg);
        $(".chatlog").append("\n\
                        <div class=\"chat-row\">\n\
                        <div class=\"chat-text\">" + data.msg + "\n\
                        </div>");

    });
    socket.on('disconnect', function () {
    });
});

的console.log:

root@wk11:/var/www/html/wchat/nodejs# nodejs server.js
Option log level is not valid. Please refer to the README.
Server up and running...
redis connection message:{"msg":"56151810ceff6 is connecting..."}
app port :: 8080
chat emtis
Missing error handler on `socket`.
Error: Connection in subscriber mode, only subscriber commands may be used
    at RedisClient.send_command (/var/www/html/wchat/nodejs/node_modules/redis/index.js:751:15)
    at RedisClient.(anonymous function) (/var/www/html/wchat/nodejs/node_modules/redis/index.js:891:21)
    at pushToRedis (/var/www/html/wchat/nodejs/server.js:71:16)
    at Socket.<anonymous> (/var/www/html/wchat/nodejs/server.js:99:9)
    at Socket.emit (events.js:95:17)
    at Socket.onevent (/var/www/html/wchat/nodejs/node_modules/socket.io/lib/socket.js:330:8)
    at Socket.onpacket (/var/www/html/wchat/nodejs/node_modules/socket.io/lib/socket.js:290:12)
    at Client.ondecoded (/var/www/html/wchat/nodejs/node_modules/socket.io/lib/client.js:193:14)
    at Decoder.Emitter.emit (/var/www/html/wchat/nodejs/node_modules/component-emitter/index.js:134:20)
    at Decoder.add (/var/www/html/wchat/nodejs/node_modules/socket.io-parser/index.js:247:12)

2 个答案:

答案 0 :(得分:1)

我在socket.io中创建了订阅者的新子对象,它将解决我的问题,如下所述。因为订阅者的父对象在io.sockets.on()中是不可访问的,而且我还为订阅者创建了另一个子对象。

/*
 *  Sender
 */
var publisher = redis.createClient();
/*
 * Receiver
 */
var subscriber = redis.createClient();

/* 
 * -------------------
 * Socket
 * -------------------
 */
io.sockets.on('connection', function (socket) {
    var _subscriber = redis.createClient();
    //console.log("app port :: " + app.get("port"));    
    console.log("Session: %j", sessionHandler);
    //console.log(JSON.stringify(sessionHandler, null, 4));
    socket.on('connection', function (data) {
        console.log('Server running on *:' + app.get('port'));
    });
    socket.on('disconnect', function () {
    });
});

答案 1 :(得分:0)

根据Redis文档:

  

一旦客户端进入订阅状态,除了额外的SUBSCRIBE,PSUBSCRIBE,UNSUBSCRIBE和PUNSUBSCRIBE命令外,它不应该发出任何其他命令。

链接:http://redis.io/commands/subscribe

要发送lpush命令,您需要创建另一个未处于订阅状态的redis客户端