spiral / roadrunner-broadcast
此包已被弃用且不再维护。未建议替代包。
RoadRunner 广播插件桥接器
v2.1.0
2022-08-30 13:52 UTC
Requires
- php: >=7.4
- ext-json: *
- google/protobuf: ^3.7
- spiral/goridge: ^3.1
- spiral/roadrunner: ^2.0
- symfony/polyfill-php80: ^1.23
Requires (Dev)
- phpunit/phpunit: ^8.0
- roave/security-advisories: dev-master
- spiral/code-style: ^1.0
- symfony/var-dumper: ^5.1
- vimeo/psalm: >=4.4
This package is auto-updated.
Last update: 2023-05-12 06:31:01 UTC
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 config: {}
在 文档 页面上了解更多有关所有可用代理的信息。
配置并启动 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')); // // 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 维护。