Error: Connection in subscriber mode, only subscriber commands may be used
Error: Connection in subscriber mode, only subscriber commands may be used
我发现在Redis 中推送消息数据时出错。请给我一些关于错误的指导。为什么会这样?我想使用nodejs将聊天数据存储在redis中。
server.js(服务器):
/*
* -------------------
* 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(客户端):
$(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)
我已经在 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 () {
});
});
根据 Redis 文档:
Once the client enters the subscribed state it is not supposed to issue any other commands, except for additional SUBSCRIBE, PSUBSCRIBE, UNSUBSCRIBE and PUNSUBSCRIBE commands.
Link: http://redis.io/commands/subscribe
为了发送 lpush 命令,您需要创建另一个未处于订阅状态的 redis 客户端
我发现在Redis 中推送消息数据时出错。请给我一些关于错误的指导。为什么会这样?我想使用nodejs将聊天数据存储在redis中。
server.js(服务器):
/*
* -------------------
* 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(客户端):
$(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)
我已经在 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 () {
});
});
根据 Redis 文档:
Once the client enters the subscribed state it is not supposed to issue any other commands, except for additional SUBSCRIBE, PSUBSCRIBE, UNSUBSCRIBE and PUNSUBSCRIBE commands.
Link: http://redis.io/commands/subscribe
为了发送 lpush 命令,您需要创建另一个未处于订阅状态的 redis 客户端