jeffreyroberts/phergie-irc-client-react
Composer 安装命令:
composer require jeffreyroberts/phergie-irc-client-react
包简介
IRC client library built on React
README 文档
README
A bare-bones PHP-based IRC client library built on React.
Install
The recommended method of installation is through composer.
composer require phergie/phergie-irc-client-react
Design goals
- Minimalistic and extensible design
- Informative logging of client-server interactions
- Simple easy-to-understand API
Usage
This example makes the bot greet other users as they join any of the channels in which the bot is present.
<?php $connection = new \Phergie\Irc\Connection(); // ... $client = new \Phergie\Irc\Client\React\Client(); $client->on('irc.received', function($message, $write, $connection, $logger) { if ($message['command'] !== 'JOIN') { return; } $channel = $message['params']['channels']; $nick = $message['nick']; $write->ircPrivmsg($channel, 'Welcome ' . $nick . '!'); }); $client->run($connection); // Also works: // $client->run(array($connection1, ..., $connectionN)); // Also possible: // Don't autorun the event loopin case you pass your own event loop and run it yourself // $client->run($connection, false);
- Create and configure an instance of the connection class,
\Phergie\Irc\Connection, for each server the bot will connect to. See phergie-irc-connection documentation for more information on configuring connection objects. - Create an instance of the client class,
\Phergie\Irc\Client\React\Client. - Call the client object's
on()method any number of times, each time specifying an event to monitor and a callback that will be executed whenever that event is received from the server. - Call the client object's
run()method with a connection object or array of multiple connection objects created in step #1.
Client Events
Below are the events supported by the client. Its on() method can be used to add callbacks for them.
connect.before.all
Emitted before any connections are established.
Parameters
\Phergie\Irc\ConnectionInterface[] $connections- array of all connection objects
Example
<?php $client->on('connect.before.all', function(array $connections) { // ... });
connect.after.all
Emitted after all connections are established.
Parameters
\Phergie\Irc\ConnectionInterface[] $connections- array of all connection objects\Phergie\Irc\Client\React\WriteStream[] $writes- corresponding array of connection write streams
Note that if a connection attempt failed, the value in $writes for that connection will be null.
Example
<?php $client->on('connect.after.all', function(array $connections, array $writes) { // ... });
connect.before.each
Emitted before each connection is established.
Parameters
\Phergie\Irc\ConnectionInterface $connection- object for the connection to be established
Example
<?php $client->on('connect.before.each', function(\Phergie\Irc\ConnectionInterface $connection) { // ... });
connect.after.each
Emitted after each connection is established.
One potentially useful application of this is to institute a delay between connections in cases where the client is attempting to establish multiple connections to the same server and that server throttles connection attempts by origin to prevent abuse, DDoS attacks, etc.
Parameters
\Phergie\Irc\ConnectionInterface $connection- object for the established connection\Phergie\Irc\Client\React\WriteStream|null $write- write stream for the connection
Note that if the connection attempt failed, $write will be null.
Example
<?php $client->on('connect.after.each', function(\Phergie\Irc\ConnectionInterface $connection, \Phergie\Irc\Client\React\WriteStream $write) { // ... });
connect.error
Emitted when an error is encountered on a connection.
Parameters
Exception $exception- exception describing the error that encountered\Phergie\Irc\ConnectionInterface $connection- container that stores metadata for the connection on which the event occurred and implements the interface\Phergie\Irc\ConnectionInterface(see its source code for a list of available methods)\Psr\Log\LoggerInterface $logger- logger for logging any relevant events from the listener which go to stdout by default (see the Monolog documentation for more information)
Example
<?php $client->on('connect.error', function( \Exception $message, \Phergie\Irc\ConnectionInterface $connection, \Psr\Log\LoggerInterface $logger ) use ($client) { $logger->debug('Connection to ' . $connection->getServerHostname() . ' lost: ' . $e->getMessage()); });
connect.end
Emitted when a connection is terminated.
This can be useful for re-establishing a connection if it is unexpectedly terminated.
Parameters
\Phergie\Irc\ConnectionInterface $connection- container that stores metadata for the connection that was terminated and implements the interface\Phergie\Irc\ConnectionInterface(see its source code for a list of available methods)\Psr\Log\LoggerInterface $logger- logger for logging any relevant events from the listener which go to stdout by default (see the Monolog documentation for more information)
Example
<?php $client->on('connect.end', function(\Phergie\Irc\ConnectionInterface $connection, \Psr\Log\LoggerInterface $logger) use ($client) { $logger->debug('Connection to ' . $connection->getServerHostname() . ' lost, attempting to reconnect'); $client->addConnection($connection); });
irc.received
Emitted when an IRC event is received from the server.
Parameters
array $message- associative array containing data for the event received from the server as obtained by\Phergie\Irc\Parser(see its documentation for examples)\Phergie\Irc\Client\React\WriteStream $write- stream that will send new events from the client to the server when its methods are called and implements the interface\Phergie\Irc\GeneratorInterface(see its source code for a list of available methods)\Phergie\Irc\ConnectionInterface $connection- container that stores metadata for the connection on which the event occurred and implements the interface\Phergie\Irc\ConnectionInterface(see its source code for a list of available methods)\Psr\Log\LoggerInterface $logger- logger for logging any relevant events from the listener which go to stdout by default (see the Monolog documentation for more information)
Example
<?php $client->on('irc.received', function( array $message, \Phergie\Irc\Client\React\WriteStream $write, \Phergie\Irc\ConnectionInterface $connection, \Psr\Log\LoggerInterface $logger ) { // ... });
irc.sent
Emitted when an IRC event is sent by the client to the server.
Parameters
string $message- message being sent by the client\Phergie\Irc\Client\React\WriteStream $write- stream that will send new events from the client to the server when its methods are called and implements the interface\Phergie\Irc\GeneratorInterface(see its source code for a list of available methods)\Phergie\Irc\ConnectionInterface $connection- container that stores metadata for the connection on which the event occurred and implements the interface\Phergie\Irc\ConnectionInterface(see its source code for a list of available methods)\Psr\Log\LoggerInterface $logger- logger for logging any relevant events from the listener which go to stdout by default (see the Monolog documentation for more information)
Example
<?php $client->on('irc.sent', function( $message, \Phergie\Irc\Client\React\WriteStream $write, \Phergie\Irc\ConnectionInterface $connection, \Psr\Log\LoggerInterface $logger ) { // ... });
irc.tick
Emitted periodically on each connection to allow events to be sent
asynchronously versus in response to received or sent events. The interval
between invocations is specified in seconds and set using the client's
setTickInterval() method.
Parameters
\Phergie\Irc\Client\React\WriteStream $write- stream that will send new events from the client to the server when its methods are called and implements the interface\Phergie\Irc\GeneratorInterface(see its source code for a list of available methods)\Phergie\Irc\ConnectionInterface $connection- container that stores metadata for the connection on which the event occurred and implements the interface\Phergie\Irc\ConnectionInterface(see its source code for a list of available methods)\Psr\Log\LoggerInterface $logger- logger for logging any relevant events from the listener which go to stdout by default (see the Monolog documentation for more information)
Example
<?php $client->on('irc.tick', function( \Phergie\Irc\Client\React\WriteStream $write, \Phergie\Irc\ConnectionInterface $connection, \Psr\Log\LoggerInterface $logger ) { // ... });
Timers
In some cases, it's desirable to execute a callback on a specified interval rather than in response to a specific event.
One-Time Callbacks
To add one-time callbacks that execute after a specified amount of time (in seconds):
<?php $client->addTimer(5, function() { // ... });
The above example will execute the specified callback at least 5 seconds after it's added.
Recurring Callbacks
To add recurring callbacks that execute on a specified interval (in seconds):
<?php $client->addPeriodicTimer(5, function() { // ... });
The above example will execute the specified callback at least every 5 seconds after it's added.
Connection Options
force-ip4
Connection sockets will use IPv6 by default where available. If you need to force usage of IPv4, set this option to true.
<?php $connection->setOption('force-ipv4', true);
allow-self-signed
By default all ssl connections only accept officially signed certificates. Sometimes you need to connect to a irc server that uses a self signed certificate. If you need to allow connections to servers using a self signed certificate, set this option to true.
<?php $connection->setOption('allow-self-signed', true);
transport
By default, a standard TCP socket is used. For IRC servers that support TLS or SSL, specify an appropriate transport.
<?php $connection->setOption('transport', 'ssl');
Tests
To run the unit test suite:
curl -s https://getcomposer.org/installer | php
php composer.phar install
./vendor/bin/phpunit
License
Released under the BSD License. See LICENSE.
Community
Check out #phergie on irc.freenode.net.
jeffreyroberts/phergie-irc-client-react 适用场景与选型建议
jeffreyroberts/phergie-irc-client-react 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 60 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 10 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「client」 「irc」 「react」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jeffreyroberts/phergie-irc-client-react 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jeffreyroberts/phergie-irc-client-react 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jeffreyroberts/phergie-irc-client-react 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Async Reactive Postgres Driver for PHP (Non-blocking)
Small utility to connect with an irker service.
Mock PSR-18 HTTP client
Asynchronous MQTT client built on React
Laravel integration for blade-to-react
A simple, flexible Laravel package for adding polymorphic reactions to any model. React with any text: like, love, care, or anything you want.
统计信息
- 总下载量: 60
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 10
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: BSD-2-Clause
- 更新时间: 2019-10-20