As a ws/wss Client

Sometimes it is necessary to let Workerman act as a client to connect to a server using the ws/wss protocol for interaction. Below are examples.

Workerman as a ws Client

<?php
use Workerman\Worker;
use Workerman\Connection\AsyncTcpConnection;
require_once __DIR__ . '/vendor/autoload.php';

$worker = new Worker();

$worker->onWorkerStart = function($worker){

    $con = new AsyncTcpConnection('ws://echo.websocket.org:80');

    // After the WebSocket handshake is successful
    $con->onWebSocketConnect = function(AsyncTcpConnection $con, ) {
        $con->send('hello');
    };

    // When a message is received
    $con->onMessage = function(AsyncTcpConnection $con, $data) {
        echo $data;
    };

    $con->connect();
};

Worker::runAll();

Workerman as a wss (ws + ssl) Client

<?php
use Workerman\Worker;
use Workerman\Connection\AsyncTcpConnection;
require_once __DIR__ . '/vendor/autoload.php';

$worker = new Worker();

$worker->onWorkerStart = function($worker){
    // SSL requires accessing port 443
    $con = new AsyncTcpConnection('ws://echo.websocket.org:443');

    // Set to access via SSL encryption, making it a wss
    $con->transport = 'ssl';

    $con->onWebSocketConnect = function(AsyncTcpConnection $con) {
        $con->send('hello');
    };

    $con->onMessage = function(AsyncTcpConnection $con, $data) {
        echo $data;
    };

    $con->connect();
};

Worker::runAll();

Workerman as a wss (ws + ssl) Client with Local SSL Certificate

<?php
use Workerman\Worker;
use Workerman\Connection\AsyncTcpConnection;
require_once __DIR__ . '/vendor/autoload.php';

$worker = new Worker();

$worker->onWorkerStart = function($worker){
    // Set the local IP and port to access the other host and the SSL certificate
    $context_option = array(
        // SSL options, see http://php.net/manual/zh/context.ssl.php
        'ssl' => array(
            // Local certificate path. Must be in PEM format and include both the local certificate and private key.
            'local_cert'        => '/your/path/to/pemfile',
            // Password for the local_cert file.
            'passphrase'        => 'your_pem_passphrase',
            // Whether to allow self-signed certificates.
            'allow_self_signed' => true,
            // Whether to verify the SSL certificate.
            'verify_peer'       => false
        )
    );

    // SSL requires accessing port 443
    $con = new AsyncTcpConnection('ws://echo.websocket.org:443', $context_option);

    // Set to access via SSL encryption, making it a wss
    $con->transport = 'ssl';

    $con->onWebSocketConnect = function(AsyncTcpConnection $con) {
        $con->send('hello');
    };

    $con->onMessage = function(AsyncTcpConnection $con, $data) {
        echo $data;
    };

    $con->connect();
};

Worker::runAll();

Other Settings

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

use Workerman\Protocols\Ws;
use Workerman\Worker;
use Workerman\Connection\AsyncTcpConnection;

$worker = new Worker();
// When the process starts
$worker->onWorkerStart = function()
{
    // Connect to remote websocket server using websocket protocol
    $ws_connection = new AsyncTcpConnection("ws://127.0.0.1:1234");
    // Send a heartbeat with opcode 0x9 to the server every 55 seconds
    $ws_connection->websocketPingInterval = 55;
    // Custom HTTP headers
    $ws_connection->headers = ['token' => 'value'];
    // Set data type, default is BINARY_TYPE_BLOB for text
    $ws_connection->websocketType = Ws::BINARY_TYPE_BLOB; // BINARY_TYPE_BLOB for text, BINARY_TYPE_ARRAYBUFFER for binary
    // After TCP completes the three-way handshake
    $ws_connection->onConnect = function($connection){
        echo "tcp connected\n";
    };
    // After the websocket handshake completes
    $ws_connection->onWebSocketConnect = function(AsyncTcpConnection $con, $response) {
        echo $response;
        $con->send('hello');
    };
    // When the remote websocket server sends a message
    $ws_connection->onMessage = function($connection, $data){
        echo "recv: $data\n";
    };
    // When an error occurs during connection, generally a failure to connect to the remote websocket server
    $ws_connection->onError = function($connection, $code, $msg){
        echo "error: $msg\n";
    };
    // When the connection to the remote websocket server is closed
    $ws_connection->onClose = function($connection){
        echo "connection closed and try to reconnect\n";
        // If the connection is closed, reconnect after 1 second
        $connection->reConnect(1);
    };
    // After setting up all the above callbacks, execute the connection operation
    $ws_connection->connect();
};
Worker::runAll();