valeriitropin/socketio-redis-adapter

1.0.0 2017-12-18 20:30 UTC

This package is not auto-updated.

Last update: 2024-09-29 05:01:41 UTC


README

socket.io redis适配器的部分移植,异步,允许获取客户端/房间并管理它们。本项目的目标是实现php和socket.io应用程序之间的通信。基于ReactPHP组件构建。

安装

composer require valeriitropin/socketio-redis-adapter

如何使用

use React\EventLoop\Factory as ReactFactory;
use ValeriiTropin\Socketio\RedisAdapter;
use Clue\React\Block;
use React\Promise;

$loop = ReactFactory::create();
$client = new RedisAdapter($loop);

$promise = $client->allRooms()->then(function ($rooms) use ($client) {
    $promises = [];
    foreach ($rooms as $room) {
        $promises[] = $client->clients([$room])->then(function ($clients) use ($room) {
            foreach ($clients as $client) {
                echo $room . ' ' . $client . "\n";
            }
        });
    }
    return Promise\all($promises);
})->otherwise(function ($error) {
    echo ($error->getMessage()) . "\n";
});

Block\await($promise, $loop);

API

RedisAdapter

__construct(React\EventLoop\LoopInterface $loop, $options = [])

$options
  • prefix:pub/sub事件前缀(socket.io
  • requestsTimeout:超时时间(毫秒),浮点数(5
  • namespace:socket.io命名空间(/
  • pubClient:pub客户端
  • subClient:sub客户端
  • customHook:可调用
  • uri:Redis连接字符串,见文档localhost

clients($rooms = []): React\Promise\Promise

返回连接到rooms的所有节点的客户端ID列表。

$adapter->clients($rooms)
    ->then(function ($clients) {
        var_dump($clients);
    })
    ->otherwise(function ($error) {
        echo ($error->getMessage()) . "\n";
    });

clientRooms($id): React\Promise\Promise

返回具有给定ID的客户端已加入的房间列表(包括其他节点)。

$adapter->clients($id)
    ->then(function ($rooms) {
        var_dump($rooms);
    })
    ->otherwise(function ($error) {
        echo ($error->getMessage()) . "\n";
    });

allRooms(): React\Promise\Promise

返回所有节点的所有房间列表。

$adapter->allRooms()
    ->then(function ($allRooms) {
        var_dump($allRooms);
    })
    ->otherwise(function ($error) {
        echo ($error->getMessage()) . "\n";
    });

remoteJoin($id, $room): React\Promise\Promise

$adapter->remoteJoin($id, $room)
    ->then(function () {})
    ->otherwise(function ($error) {
        echo ($error->getMessage()) . "\n";
    });

remoteLeave($id, $room): React\Promise\Promise

$adapter->remoteLeave($id, $room)
    ->then(function () {})
    ->otherwise(function ($error) {
        echo ($error->getMessage()) . "\n";
    });

remoteDisconnect($id, $close): React\Promise\Promise

$adapter->remoteDisconnect($id, $close)
    ->then(function () {})
    ->otherwise(function ($error) {
        echo ($error->getMessage()) . "\n";
    });

customRequest($data): React\Promise\Promise

向所有节点发送请求,这些节点将通过customHook方法做出响应。

$adapter->customRequest($data)
    ->then(function ($replies) {})
    ->otherwise(function ($error) {
        echo ($error->getMessage()) . "\n";
    });

getLoop(): React\EventLoop\LoopInterface

返回循环实例。

getPub(): Clue\React\Redis\StreamingClient

返回pub客户端。

getSub(): Clue\React\Redis\StreamingClient

返回sub客户端。

unsubscribeFromRequestChannel(): React\Promise\Promise

取消适配器实例对请求通道的订阅。

链接