0

我尝试使用 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));
4

0 回答 0