rivulent/socket.io-php-emitter

已维护* socket.io-emitter 的 PHP 实现

0.2 2023-05-15 20:40 UTC

This package is auto-updated.

Last update: 2024-09-15 23:37:53 UTC


README

Build Status

维护分支

这是一个 rase-/socket.io-php-emitter 的分支。
由于原始存储库不再维护,并且与新版本的 Socket.io 存在兼容性问题,我建议使用此存储库,并在此处提交问题。

@ashiina

socket.io-php-emitter

A PHP implementation of socket.io-emitter.

安装和设置

1. socket.io-php-emitter

要将它作为 composer package 安装并用于您的 PHP 项目中,使用 composer install 安装依赖项。

2. Redis 服务器

您需要一个 Redis 服务器来发射事件。设置您的 Redis 服务器。

3. socket.io-redis (node.js)

要接收发射的事件,您必须使用 socket.io-redis。它将自动处理从 socket.io-php-emitter 发射的事件。从这里安装和设置:https://npmjs.net.cn/package/socket.io-redis

用法

初始化

$redis = new \Redis(); // Using the Redis extension provided client
$redis->connect('127.0.0.1', '6379');
$emitter = new SocketIO\Emitter($redis);
$emitter->emit('event', 'payload str');

广播和其他标志

可能的标志

  • json
  • volatile
  • broadcast
// Below initialization will create a  phpredis client, or a TinyRedisClient depending on what is installed
$emitter = new SocketIO\Emitter(array('port' => '6379', 'host' => '127.0.0.1'));
// broadcast can be replaced by any of the other flags
$emitter->broadcast->emit('other event', 'such data');

发射对象

$emitter = new SocketIO\Emitter(); // If arguments are not provided, they will default to array('port' => '6379', 'host' => '127.0.0.1')
$emitter->emit('event', array('property' => 'much value', 'another' => 'very object'));

关于 PHP Redis 客户端

此项目需要一个 Redis 客户端用于 PHP。如果您没有安装 PECL Redis,发射器将默认使用 TinyRedisClient。但是,您也可以传递任何支持 publish 方法的 Redis 客户端。

开发

要运行测试,调用 make test。当前的测试套件将只检查 redis 监视器以确保所有内容都正确发布。未来将投入更多工作以创建更好的集成测试套件。