clue/multicast-react
Composer 安装命令:
composer require clue/multicast-react
包简介
Simple, event-driven multicast UDP message client and server for ReactPHP.
关键字:
README 文档
README
Simple, event-driven multicast UDP message client and server for ReactPHP.
Multicast UDP messages are needed for quite a few (low-level) networking protocols. Among others, multicast networking is the basis for mDNS (Multicast DNS), HTTPMU (Multicast UDP HTTP Messages), UPnP/SSDP (Universal Plug and Play / Simple Service Discovery Protocol) and others. This library exposes a simple subset of commonly needed functionality for multicast networking through an easy to use API.
Table of contents
Support us
We invest a lot of time developing, maintaining and updating our awesome open-source projects. You can help us sustain this high-quality of our work by becoming a sponsor on GitHub. Sponsors get numerous benefits in return, see our sponsoring page for details.
Let's take these projects to the next level together! 🚀
Quickstart example
Once installed, you can use the following code to create a simple echo server that listens for incoming multicast messages:
<?php require __DIR__ . '/vendor/autoload.php'; $factory = new Clue\React\Multicast\Factory(); $socket = $factory->createReceiver('224.10.20.30:4050'); $socket->on('message', function ($data, $remote) use ($socket) { echo 'Sending back ' . strlen($data) . ' bytes to ' . $remote . PHP_EOL; $socket->send($data, $remote); });
See also the examples.
Usage
Factory
The Factory is responsible for creating your SocketInterface instances.
$factory = new Clue\React\Multicast\Factory();
This class takes an optional LoopInterface|null $loop parameter that can be used to
pass the event loop instance to use for this object. You can use a null value
here in order to use the default loop.
This value SHOULD NOT be given unless you're sure you want to explicitly use a
given event loop instance.
createSender()
The createSender(): SocketInterface method can be used to
create a socket capable of sending outgoing multicast datagrams and receiving
incoming unicast responses. It returns a SocketInterface instance.
$socket = $factory->createSender(); // send a multicast message to everybody listening on the given address $socket->send('hello?', '224.10.20.30:4050'); // report incoming unicast replies $socket->on('message', function ($data, $address) { echo 'received ' . strlen($data) . ' bytes from ' . $address . PHP_EOL; });
This method works on PHP versions as old as PHP 5.3 (and up), as its socket API has always been level 1 multicast conformant.
createReceiver()
The createReceiver(string $address): SocketInterface method can be used to
create a socket capable of receiving incoming multicast datagrams and sending
outgoing unicast or multicast datagrams. It returns a SocketInterface instance.
$socket = $factory->createReceiver('224.10.20.30:4050'); // report incoming multicast messages $socket->on('message', function ($data, $remote) use ($socket) { echo 'Sending back ' . strlen($data) . ' bytes to ' . $remote . PHP_EOL; // send a unicast reply to the remote $socket->send($data, $remote); });
This method requires PHP 5.4+ and ext-sockets.
Otherwise, it will throw a BadMethodCallException.
This is a requirement because receiving multicast datagrams requires a
level 2 multicast conformant
socket API.
The required multicast socket options and constants have been added with PHP 5.4+.
These options are only available to the low level socket API (ext-sockets), not
to the newer stream based networking API.
Internally, this library uses a workaround to create stream based sockets and then sets the required socket options on its underlying low level socket resource. This is done because ReactPHP is built around the general purpose stream based API and has only somewhat limited support for the low level socket API.
SocketInterface
The Factory creates instances of the React\Datagram\SocketInterface
from the react/datagram package.
This means that you can use all its normal methods like so:
$socket->send($message, $address); $socket->on('message', function ($message, $address) { }); $socket->on('close', function() { }); $socket->pause(); $socket->resume(); $socket->end(); $socket->close();
Please refer to the datagram documentation for more details.
Install
The recommended way to install this library is through Composer. New to Composer?
This project follows SemVer. This will install the latest supported version:
composer require clue/multicast-react:^1.2
See also the CHANGELOG for details about version upgrades.
This project aims to run on any platform and thus does not require any PHP extensions and supports running on legacy PHP 5.3 through current PHP 8+ and HHVM. It's highly recommended to use the latest supported PHP version for this project.
The createSender() method works on all supported platforms
without any additional requirements. However, the createReceiver()
method requires PHP 5.4 (or up) and ext-sockets. See above for more details.
Tests
To run the test suite, you first need to clone this repo and then install all dependencies through Composer:
composer install
To run the test suite, go to the project root and run:
vendor/bin/phpunit
License
This project is released under the permissive MIT license.
Did you know that I offer custom development services and issuing invoices for sponsorships of releases and for contributions? Contact me (@clue) for details.
clue/multicast-react 适用场景与选型建议
clue/multicast-react 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 75.52k 次下载、GitHub Stars 达 25, 最近一次更新时间为 2015 年 03 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「async」 「udp」 「reactphp」 「multicast」 「mcast」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 clue/multicast-react 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 clue/multicast-react 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 clue/multicast-react 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
The bundle for easy using json-rpc api on your project
An async event for hyperf.
A PHP PSR-3 compliant logger to send data to graylog2 (GELF format)
A Laravel package to send logs to a gelf compatible backend like graylog
A decorator of the ReactPHP MySQL connection interface which augments its behaviour, e.g. providing binding of associative parameters.
Zenity allows you to build graphical desktop (GUI) applications in PHP, built on top of ReactPHP.
统计信息
- 总下载量: 75.52k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 25
- 点击次数: 5
- 依赖项目数: 6
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-03-23