一个支持在 Swoole 或其它非 Workerman 环境,开发 Gateway Worker 的组件。

Overview

Workerman Gateway SDK

一个支持在 Swoole 或其它非 Workerman 环境,开发 Gateway Worker 的组件。

支持用 Workerman Gateway 做网关,Swoole 编写业务代码。

安装

composer require yurunsoft/workerman-gateway-sdk

Swoole Demo

<?php

declare(strict_types=1);

use GatewayWorker\Lib\Context;
use GatewayWorker\Lib\Gateway;
use GatewayWorker\Protocols\GatewayProtocol;
use Swoole\Coroutine;
use Swoole\Coroutine\Channel;
use function Swoole\Coroutine\parallel;
use Workerman\Gateway\Config\GatewayWorkerConfig;
use Workerman\Gateway\Gateway\Contract\IGatewayClient;
use Workerman\Gateway\Gateway\GatewayWorkerClient;

require dirname(__DIR__) . '/vendor/autoload.php';

Co\run(function () {
    $channel = new Channel(1024);

    Coroutine::create(function () use ($channel) {
        // 通过 Channel 实现单进程多协程任务处理
        parallel(swoole_cpu_num(), function () use ($channel) {
            while (true)
            {
                $result = $channel->pop();
                if (false === $result)
                {
                    break;
                }
                switch ($result['type'])
                {
                    case 'onException':
                        /** @var Throwable $th */
                        ['th' => $th] = $result['data'];
                        // 异常处理
                        var_dump($th->getMessage(), $th->getTraceAsString());
                        break;
                    case 'onGatewayMessage':
                        /** @var IGatewayClient $client */
                        ['client' => $client, 'message' => $message] = $result['data'];
                        var_dump($message);
                        $clientId = Context::addressToClientId($message['local_ip'], $message['local_port'], $message['connection_id']);
                        switch ($message['cmd']) {
                            case GatewayProtocol::CMD_ON_CONNECT:
                                // 连接
                                var_dump('connect:' . $clientId);
                                break;
                            case GatewayProtocol::CMD_ON_MESSAGE:
                                var_dump('message:' . $clientId, 'body:' . $message['body']);
                                $data = json_decode($message['body'], true);
                                switch ($data['action'] ?? '')
                                {
                                    case 'send':
                                        // {"action":"send", "content":"test content"}
                                        // 广播给所有用户
                                        Gateway::sendToAll(json_encode([
                                            'action'  => 'receive',
                                            'content' => $data['content'] ?? '',
                                        ]));
                                        break;
                                }
                                break;
                            case GatewayProtocol::CMD_ON_CLOSE:
                                var_dump('close:' . $clientId);
                                break;
                            case GatewayProtocol::CMD_ON_WEBSOCKET_CONNECT:
                                var_dump('websocket connect:' . $clientId, 'body:', $message['body']);
                                break;
                        }
                        break;
                }
            }
        });
    });

    $config = new GatewayWorkerConfig();
    $config->setRegisterAddress('127.0.0.1:1238');

    // Gateway Client 配置
    Gateway::$registerAddress = $config->getRegisterAddress();

    $workerKey = getmypid() . '-' . Coroutine::getuid();
    // Gateway Worker
    $client = new GatewayWorkerClient($workerKey, $config);
    // 异常处理
    $client->onException = function (Throwable $th) use ($channel) {
        $channel->push([
            'type' => 'onException',
            'data' => [
                'th' => $th,
            ],
        ]);
    };
    // 网关消息
    $client->onGatewayMessage = function (IGatewayClient $client, array $message) use ($channel) {
        $channel->push([
            'type' => 'onGatewayMessage',
            'data' => [
                'client'  => $client,
                'message' => $message,
            ],
        ]);
    };
    $client->run();
});
You might also like...
The whmcs payment module for TigoPesa Payment Gateway.

whmcs-tigopesa INSTALLATION INSTRUCTIONS TIGOPESA WHMCS Please follow the instructions below to setup the whmcs-tigopesa gateway module. Download zipp

A payment gateway plugin for WooCommerce to see if your checkout works.

=== Order Test For All for WooCommerce === Contributors: ikamal Donate link: https://kamal.pw/ Tags: wc order test, wc order, woocommerce, woocommerce

Simple laravel package for 2C2P Payment Gateway.

Laravel 2C2P Payment Gateway Simple laravel package for 2C2P Payment Gateway. Installation You can install the package via composer: composer require

With the help of the Laravel eCommerce CashU Payment Gateway, the admin can integrate the CashU payment method in the Bagisto store.

Introduction Bagisto CashU Payment add-on allow customers to pay for others using CashU payment gateway. Requirements: Bagisto: v1.3.2 Installation wi

Integrate Paytm Payment gateway to php website.

paytm-integration-kit-project-for-PHP Integrate Paytm Payment gateway to php website. Copy PaytmKit folder in document root of your server (like /var/

Provides Bitcoin-Lightning payment gateway powered by Strike
Provides Bitcoin-Lightning payment gateway powered by Strike

Bitcoin Payments - Powered by Strike Drupal module to allow user to pay with bitcoin on your Commerce website using Strike API. Strike Js is used for

ShurjoPay payment gateway integration for PHP applications.

ShurjoPay PHP Library Using this library you can integrate ShurjoPay payment gateway into your PHP applications. If you face any problem then create i

uPay(bangladesh) payment gateway integration package for laravel

uPay BD Payment Gateway Requirements PHP =7.2 Laravel = 6 Installation composer require codeboxr/upay vendor publish (config) php artisan vendor:pub

MOKA > Opencart 2.3 Payment Gateway
MOKA Opencart 2.3 Payment Gateway

Moka - Opencart Payment Gateway Requirements PHP 5.6.0 and later. Dependencies The bindings require the following extensions in order to work properly

Releases(v1.0.0)
Owner
Yurun
PHP 程序员,imi 框架作者,宇润 PHP 全家桶作者。 我们程序员,外修语言,内修算法。以数据为根,算天算地算自己。 程序员♂♀聚集地Q群:74401592
Yurun
The Laravel eCommerce ABA Payment Gateway module allows the admin to integrate the ABA payment gateway to the online store.

Introduction Bagisto ABA Payment Gateway. Requirements: Bagisto: v1.3.2. Installation with composer: Run the following command composer require bagist

Bagisto 3 May 31, 2022
A plugin that adds worker entities to minecraft.

WorkersDemo A plugin that adds worker entities to minecraft. Workers does things that players does such as mining wood/stone etc. How to use? You can

Oğuzhan 6 Dec 17, 2021
Fully covered with tests, documented by Swagger and dockerized API based on enterprise-level framework with optional queue worker.

symfony-api Fully covered with tests, documented by Swagger and dockerized API based on enterprise-level framework with optional queue worker. ⚙️ Depl

Oleksii Velychko 1 Nov 20, 2022
Websocket chat room written in PHP based on workerman.

基于workerman的GatewayWorker框架开发的一款高性能支持分布式部署的聊天室系统。

walkor 1.1k Jan 8, 2023
PHP's Promse implementation depends on the Swoole module.

php-promise-swoole PHP's Promse implementation depends on the Swoole module. Promise::allsettled([ /** Timer 调用 */ /** Timer call */

拓荒者 3 Mar 15, 2022
Php-gamer - A repo with PHP 8.1, Swoole and Laminas. And of course, Docker in the front.

PHP-Gamer Instructions for run this app: First time $ git clone [email protected]:fatorx/php-gamer.git $ cd php-gamer $ chmod +x docker-build.sh $ chmod

Fabio de Souza 6 Oct 6, 2022
A framework agnostic, multi-gateway payment processing library for PHP 5.6+

Omnipay An easy to use, consistent payment processing library for PHP Omnipay is a payment processing library for PHP. It has been designed based on i

The League of Extraordinary Packages 5.7k Dec 30, 2022
WHMCS Payment Gateway Module for Coinify

vrcoinify WHMCS Payment Gateway Module for Coinify Installing guide You should copy all contents from module folder to your WHMCS application folder u

VR DEV TEAM 3 Mar 15, 2022
Xenon\LaravelBDSms is a sms gateway package for sending text message to Bangladeshi mobile numbers using several gateways like sslcommerz, greenweb, dianahost,metronet in Laravel framework

Xenon\LaravelBDSms is a sms gateway package for sending text message to Bangladeshi mobile numbers using several gateways for Laravel. You should use

Ariful Islam 95 Jan 3, 2023
A site to sell an E-book with paystack payment gateway integrated

YoungAndGetRich This is a platform to sell the named book using paystack payment gateway. Technology used are: MySql database PHP HTML5 CSS3 Bootstrap

Abiodun Sam 1 Oct 26, 2021