stanx/fabiang-xmpp

XMPP协议(Jabber)连接库

0.8.1 2021-10-26 19:52 UTC

This package is auto-updated.

Last update: 2024-09-27 02:19:20 UTC


README

PHP的XMPP协议(Jabber)连接库。

https://github.com/tohenk/xmpp 分支而来(再分支自 https://github.com/fabiang/xmpp

系统要求

  • PHP >= 7.0
  • psr/log
  • (可选) psr/log-implementation - 类似于 monolog/monolog 的日志记录

安装

初次使用Composer?请阅读 介绍。将以下内容添加到您的composer文件中

composer require stanx/fabiang-xmpp

文档

此库使用对象来保存选项

use Fabiang\Xmpp\Options;
$options = new Options($address);
$options->setUsername($username)
        ->setPassword($password)
        ->setTimeout(10);

服务器地址必须是 tcp://myjabber.com:5222 格式。
如果服务器支持TLS,连接将自动加密。

您也可以将PSR-2兼容的对象传递给选项对象

$options->setLogger($logger)

客户端管理与Jabber服务器的连接,并需要选项对象

use Fabiang\Xmpp\Client;
$client = new Client($options);
// optional connect manually
$client->connect();

发送数据时,只需传递实现 Fabiang\Xmpp\Protocol\ProtocolImplementationInterface 的对象

use Fabiang\Xmpp\Protocol\Roster;
use Fabiang\Xmpp\Protocol\Presence;
use Fabiang\Xmpp\Protocol\Message;

// fetch roster list; users and their groups
$client->send(new Roster);
// set status to online
$client->send(new Presence);

// send a message to another user
$message = new Message;
$message->setMessage('test')
        ->setTo('nickname@myjabber.com');
$client->send($message);

// join a channel
$channel = new Presence;
$channel->setTo('channelname@conference.myjabber.com')
        ->setPassword('channelpassword')
        ->setNickName('mynick');
$client->send($channel);

// send a message to the above channel
$message = new Message;
$message->setMessage('test')
        ->setTo('channelname@conference.myjabber.com')
        ->setType(Message::TYPE_GROUPCHAT);
$client->send($message);

最后,您应该断开连接

$client->disconnect();

BSD-2-Clause。请参阅 LICENSE