PHP code example of naka1205 / phpsocket

1. Go to this page and download the library: Download naka1205/phpsocket 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/ */

    

naka1205 / phpsocket example snippets



Naka507\Socket\Server;
$server = new Server();

//服务启动
$server->onWorkerStart = function($worker)
{
    echo "New onWorkerStart\n";
};

//建立连接
$server->onConnect = function($connection)
{
    echo "New Connection\n";
};

//接收请求
$server->onMessage = function($request, $response)
{
    $response->write(' Hello World !!!');
    $response->end();
};
$server->start();


Naka507\Socket\Server;
$opt = array(
    'ssl' => array(
        // 请使用绝对路径
        'local_cert'                 => '/***/fullchain.pem',
        'local_pk'                   => '/***/privkey.pem',
        'verify_peer'                => false,
        'allow_self_signed' 		 => true 
    )
);
$server = new Server(443,$opt);
$server->transport = 'ssl';

//服务启动
$server->onWorkerStart = function($worker)
{
    echo "New onWorkerStart\n";
};

//建立连接
$server->onConnect = function($connection)
{
    echo "New Connection\n";
};

//接受请求
$server->onMessage = function($request, $response)
{
    $response->write(' SSL: Hello World !!!');
    $response->end();
};
$server->start();

php app.php

php app.php

php app.php -d

php app.php -s