我尝试使用nodejs和redis将通知推送到特定客户端,但向请求的用户发出通知无法正常工作
我在php和nodejs和客户端的代码如下:
//Nodejs Server ---------------------------
var io = require('socket.io').listen(3000);
var redis = require('redis');
var $clients = {};
var $notifySubscribe = redis.createClient();
var $loginUserSubscribe = redis.createClient();
$notifySubscribe.subscribe('notify');
$loginUserSubscribe.subscribe('loginUser');
$notifySubscribe.on('message', function($channel, $message) {
var $data = JSON.parse($message);
if($data.userId in $clients){
$clients[$data.userId].emit('updateNotifications', '+1');
}
});
io.sockets.on('connection', function(socket) {
$loginUserSubscribe.on('message', function($channel, $message){
var $data = JSON.parse($message);
if(!($data.userId in $clients)){
$clients[$data.userId] = socket;
}
});
});
//Client ----------------------------------
socketIo.on('updateNotifications', function($count){
alert($count);
});
//Php Server ------------------------------
$data = array('userId' => 20584, 'username' => 'mohammadsaleh');
$this->__redis->publish("notify", json_encode($data));