PHP Websocket服务器订阅了redis

时间:2015-01-21 13:05:10

标签: php websocket redis predis

这是我的第一个问题。 :) 所以我的问题是: 我有一台使用Redis(Predis)服务器的Apache服务器。还有和WebSocket服务器(PHP WebSocketServer,谁需要“订阅”Redis服务器。 对于订阅,我使用:

    $pubsub = $client->pubSubLoop();
// Subscribe to your channels
$pubsub->subscribe('control_channel', 'notifications');
// Start processing the pubsup messages. Open a terminal and use redis-cli
// to push messages to the channels. Examples:
//   ./redis-cli PUBLISH notifications "this is a test"
//   ./redis-cli PUBLISH control_channel quit_loop
foreach ($pubsub as $message) {
    switch ($message->kind) {
        case 'subscribe':
            echo "Subscribed to {$message->channel}", PHP_EOL;
            break;
        case 'message':
            if ($message->channel == 'control_channel') {
                if ($message->payload == 'quit_loop') {
                    echo "Aborting pubsub loop...", PHP_EOL;
                    $pubsub->unsubscribe();
                } else {
                    echo "Received an unrecognized command: {$message->payload}.", PHP_EOL;
                }
            } else {
                echo "Received the following message from {$message->channel}:",
                     PHP_EOL, "  {$message->payload}", PHP_EOL, PHP_EOL;
            }
            break;
    }
}

我意识到WebSocket服务器使用了一种无限循环,而Predis(订阅)是一样的。

如果我错了,请打我,或者你可以给我一些建议。

感谢。

0 个答案:

没有答案