octamp/client

PHP Swoole 的 WAMP 客户端

v1.3.0 2023-09-23 03:14 UTC

This package is auto-updated.

Last update: 2024-09-15 06:23:15 UTC


README

Packagist PHP Version License Test codecov

Octamp 客户端是 WAMP (Web Application Messaging Protocol) 的开源客户端,适用于 PHP。

Octamp 客户端使用 Open Swoole,这是一个基于事件驱动、异步、非阻塞 I/O 协程编程模型的 PHP 高性能网络框架。

我们还设计了 Octamp 客户端函数,使其与 AutobahnJS 相同。

Octamp 这个名字来源于 Octopus + WAMP

支持的 WAMP 功能

  • 发布
  • 订阅
  • 调用
  • 调用进度
  • 注册

需求

  • PHP >= 8.1
  • Swoole / OpenSwoole 扩展

安装

composer require octamp/client

示例

<?php

use Octamp\Client\Auth\WampcraAuthenticator;
use Octamp\Client\Peer;
use Octamp\Client\Session;

require_once __DIR__ . '/../../vendor/autoload.php';

\Co\run(function () {
    $client = new Peer('crossbar', 9000);
    
    $client->onOpen(function (Session $session) {
        // subscribe
        $session->subscribe('hello', function (array $args) {
            echo 'Event ' . $args[0] . PHP_EOL;
        });
    
        // publish
        $session->publish('hello', ['hello octamp'], [], ['exclude_me' => false]);
    
        // publish with acknowledgement
        $session
            ->publish('hello', ['hello octamp with acknowledgement'], [], ['acknowledge' => true, 'exclude_me' => false])
            ->then(
                function () {
                    echo 'Publish Acknowledged!' . PHP_EOL;
                },
                function ($error) {
                    echo 'Publish Error ' . $error . PHP_EOL;
                },
            );
    
        // register
        $session->register('add', function (array $args) {
            return $args[0] + $args[1];
        });
    
        // call
        $session->call('add', [1, 3])->then(function ($result) {
            echo 'Result ' . $result . PHP_EOL;
        });
    });
    
    $client->open();
});

待办事项

  • 调用取消
  • 调用超时
  • 退订
  • RPC 进度调用
  • 自动重连
  • 子协议处理
  • 心跳
  • 自定义错误处理
  • TLS 连接
  • 会话日志
  • 会话前缀
  • 基于模式的订阅/注册