PHP code example of ljhsmileking / laravel-gateway-worker

1. Go to this page and download the library: Download ljhsmileking/laravel-gateway-worker library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ljhsmileking / laravel-gateway-worker example snippets


'providers' => [
    // ...
    SmileyMrKing\GatewayWorker\GatewayWorkerServiceProvider::class,
];

$app->register(SmileyMrKing\GatewayWorker\GatewayWorkerServiceProvider::class);

return [

    /*
    |--------------------------------------------------------------------------
    | Gateway Worker Service
    |--------------------------------------------------------------------------
    */

    'default_service' => 'push', # Set Gateway::$registerAddress to push.register_address by default

    'push' => [
        'lan_ip' => env('WS_LAN_IP', '127.0.0.1'), # Internal IP, fill in the real internal IP when deploying in a multi-server distributed environment.

        'register' => env('WS_REGISTER', 'text://0.0.0.0:20000'),
        'register_address' => env('WS_REGISTER_ADDRESS', '127.0.0.1:20000'), # Registration service address

        'worker_name' => 'PushBusinessWorker', # Set the name of the BusinessWorker process
        'worker_count' => 1, # Set the number of BusinessWorker processes
        # Set which class to handle business logic. The class must implement the static method 'onMessage'. 'onConnect' and 'onClose' static methods are optional.
        'event_handler' => \SmileyMrKing\GatewayWorker\Push\PushEvent::class,

        'gateway' => env('WS_GATEWAY', 'websocket://0.0.0.0:20010'),# Address allowed for connection
        'gateway_name' => 'PushGateway', # Set the name of the Gateway process for easy statistics in the 'status' command
        'gateway_count' => 1, # Number of Gateway processes
        'start_port' => env('WS_START_PORT', '20100'),  # Starting port for listening on the local machine
        'ping_interval' => 55,  # Heartbeat interval, only for server-side heartbeat
        'ping_not_response_limit' => 1,   # 0: server actively sends heartbeat, 1: client actively sends heartbeat
        'ping_data' => '{"type":"ping"}', # Data for the server to actively send heartbeat, only for server-side heartbeat. When the client times out without sending heartbeat, the server will actively send a heartbeat detection.

        'gateway_start' => true,
        'business_worker_start' => true,
        'register_start' => true,

        'gateway_transport' => 'tcp', // When set to 'ssl', SSL will be enabled, websocket+SSL is 'wss'
        /*'gateway_context' => [
            // For more SSL options, please refer to the manual: http://php.net/manual/en/context.ssl.php
            'ssl' => array(
                // Please use absolute paths
                'local_cert' => '/your/path/of/server.pem', // It can also be a crt file
                'local_pk' => '/your/path/of/server.key',
                'verify_peer' => false,
                'allow_self_signed' => true, // Enable this option if it's a self-signed certificate
            )
        ],*/
    ],
    
    'pid_file' => null, // Custom PID file absolute path, by default in 'vendor/smileymrking/laravel-gateway-worker/src/GatewayWorker/worker' directory
    'log_file' => null, // Custom log file absolute path, same as above by default

];


return [
// ...
'demo' => [
        'lan_ip' => env('WS_LAN_IP_DEMO', '127.0.0.1'), # Internal IP, fill in the real internal IP when deploying in a multi-server distributed environment.

        'register' => env('WS_REGISTER_DEMO', 'text://0.0.0.0:20000'),
        'register_address' => env('WS_REGISTER_ADDRESS_DEMO', '127.0.0.1:20000'), # Registration service address

        'worker_name' => 'DemoBusinessWorker', # Set the name of the BusinessWorker process
        'worker_count' => 1, # Set the number of BusinessWorker processes
        # Set which class to handle business logic. The class must implement the static method 'onMessage'. 'onConnect' and 'onClose' static methods are optional.
        'event_handler' => \App\GatewayWorker\Demo\DemoEvent::class,

        'gateway' => env('WS_GATEWAY_DEMO', 'websocket://0.0.0.0:20010'),# Address allowed for connection
        'gateway_name' => 'DemoGateway', # Set the name of the Gateway process for easy statistics in the 'status' command
        'gateway_count' => 1, # Number of Gateway processes
        'start_port' => env('WS_START_PORT_DEMO', '20100'),  # Starting port for listening on the local machine
        'ping_interval' => 55,  # Heartbeat interval, only for server-side heartbeat
        'ping_not_response_limit' => 1,   # 0: server actively sends heartbeat, 1: client actively sends heartbeat
        'ping_data' => '{"type":"ping"}', # Data for the server to actively send heartbeat, only for server-side heartbeat. When the client times out without sending heartbeat, the server will actively send a heartbeat detection.

        'gateway_start' => true,
        'business_worker_start' => true,
        'register_start' => true,

        'gateway_transport' => 'tcp', // When set to 'ssl', SSL will be enabled, websocket+SSL is 'wss'
        /*'gateway_context' => [
            // For more SSL options, please refer to the manual: http://php.net/manual/en/context.ssl.php
            'ssl' => array(
                // Please use absolute paths
                'local_cert' => '/your/path/of/server.pem', // It can also be a crt file
                'local_pk' => '/your/path/of/server.key',
                'verify_peer' => false,
                'allow_self_signed' => true, // Enable this option if it's a self-signed certificate
            )
        ],*/
        'pid_file' => storage_path('logs/gateway-worker-demo.pid'),
        'log_file' => storage_path('logs/gateway-worker-demo.log'),
    ],

];


namespace App\GatewayWorker\Demo;

use SmileyMrKing\GatewayWorker\GatewayWorker\GatewayWorkerEvents;

class DemoEvent extends GatewayWorkerEvents
{
    public static function onMessage($client_id, $message)
    {
        // Do something
    }
}
shell
php artisan vendor:publish --provider="SmileyMrKing\GatewayWorker\GatewayWorkerServiceProvider"
shell
> php artisan gateway-worker push start

Workerman[gateway-worker push] start in DEBUG mode
----------------------------------------------- WORKERMAN -----------------------------------------------
Workerman version:4.0.6          PHP version:7.2.5-1+ubuntu18.04.1+deb.sury.org+1
------------------------------------------------ WORKERS ------------------------------------------------
proto   user            worker                listen                       processes    status
tcp     vagrant         PushGateway           websocket://0.0.0.0:20010    1             [OK]
tcp     vagrant         PushBusinessWorker    none                         1             [OK]
tcp     vagrant         Register              text://0.0.0.0:20000         1             [OK]
---------------------------------------------------------------------------------------------------------
Press Ctrl+C to stop. Start success.