spiral / 广播
此包已被弃用且不再维护。未建议替代包。
RoadRunner 广播插件桥接器
v2.0.5
2021-02-03 15:39 UTC
Requires
- php: >=7.2
- ext-json: *
- spiral/roadrunner: ^1.1
Requires (Dev)
- phpunit/phpunit: ~8.0
- spiral/code-style: ^1.0
README
此存储库包含广播 RoadRunner 插件的代码桥接器。
安装
要安装应用程序服务器和广播代码库
$ composer require spiral/roadrunner-broadcast
您可以使用方便的安装程序下载最新可用的兼容版本 RoadRunner 组装
$ composer require spiral/roadrunner-cli --dev $ vendor/bin/rr get
用法
例如,这样的配置相当可行来运行
rpc: listen: tcp://127.0.0.1:6001 server: # Don't forget to create a "worker.php" file command: "php worker.php" relay: "pipes" http: address: 127.0.0.1:80 # Indicate that HTTP support ws protocol middleware: [ "websockets" ] websockets: broker: default path: "/ws" broadcast: default: driver: memory
在文档页面上了解更多关于所有可用代理的信息。
配置并启动 RoadRunner 服务器后,相应的 API 将对您可用。
<?php use Spiral\Goridge\RPC\RPC; use Spiral\RoadRunner\Broadcast\Broadcast; require __DIR__ . '/vendor/autoload.php'; $broadcast = new Broadcast(RPC::create('tcp://127.0.0.1:6001')); if (!$broadcast->isAvailable()) { throw new \LogicException('The [broadcast] plugin not available'); } // // Now we can send a message to a specific topic // $broadcast->publish('channel-1', 'message for channel #1');
选择特定主题
或者,您还可以使用特定主题(或主题集)作为独立实体并直接发布到它。
// Now we can select the topic we need to work only with it $topic = $broadcast->join(['channel-1', 'channel-2']); // And send messages there $topic->publish('message'); $topic->publish(['another message', 'third message']);
在文档页面上了解更多所有可能性。
客户端
除了服务器(PHP)部分外,大多数项目中也有客户端部分。在大多数情况下,这是一个使用WebSocket协议连接到服务器的浏览器。
const ws = new WebSocket('ws://127.0.0.1/broadcast'); ws.onopen = e => { const message = { command: 'join', topics: ['channel-1', 'channel-2'] }; ws.send(JSON.stringify(message)); }; ws.onmessage = e => { const message = JSON.parse(e.data); console.log(`${message.topic}: ${message.payload}`); }
示例
示例可在相应目录./example中找到。
许可证
MIT 许可证(MIT)。有关更多信息,请参阅LICENSE
。由Spiral Scout维护。