Skip to content

AmirHkrg/OpenSwooleApp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OpenSwoole

  • Chain Methode
  • Clean Code
  • Easy To Use
  • Faster
  • Cli

You can use chain methode.

$server->Http()->end()->start();

Easier to use and requires less coding.

The two codes below do the same thing. Compare them.

Original version :
use OpenSwoole\Http\Server;
use OpenSwoole\Http\Request;
use OpenSwoole\Http\Response;

$server = new OpenSwoole\HTTP\Server("127.0.0.1", 9501);

$server->on("start", function (Server $server) {
    echo "Server Started";
});

$server->on("request", function (Request $request, Response $response) {
    $response->header("Content-Type", "text/plain");
    $response->end('Hello, OpenSwoole');
});

$server->start();
Optimized version in this repository :
$server = new OpenswooleApp('127.0.0.1', 9501);

$server->Http()->serverOnStart(function (){
    echo "Server Started";
})->end('Hello, OpenSwoole')->start();

Very easy to use.

Just create an object from your server

$server = new OpenswooleApp('127.0.0.1', 9501);

After this, you have access to all methods from $server


Much faster.

Original version benchmark :
Concurrency Level:      1000
Time taken for tests:   192.642 seconds
Complete requests:      100000
Failed requests:        0
Total transferred:      25548400000 bytes
HTML transferred:       25535300000 bytes
Requests per second:    519.10 [#/sec] (mean)
Time per request:       1926.425 [ms] (mean)
Time per request:       1.926 [ms] (mean, across all concurrent requests)
Transfer rate:          129512.52 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    3  51.9      0    1088
Processing:    46 1913 196.5   1879    3522
Waiting:       26 1912 196.4   1878    3521
Total:        110 1917 198.9   1880    3522

Percentage of the requests served within a certain time (ms)
  50%   1880
  66%   1896
  75%   1912
  80%   1923
  90%   2010
  95%   2130
  98%   2543
  99%   3011
 100%   3522 (longest request)
Optimized version in this repository benchmark :
Concurrency Level:      1000
Time taken for tests:   28.996 seconds
Complete requests:      100000
Failed requests:        0
Total transferred:      25547800000 bytes
HTML transferred:       25535300000 bytes
Requests per second:    3448.78 [#/sec] (mean)
Time per request:       289.958 [ms] (mean)
Time per request:       0.290 [ms] (mean, across all concurrent requests)
Transfer rate:          860436.03 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0   16  52.4     13    1067
Processing:    33  274  23.9    274     552
Waiting:        1   17  20.7     15     326
Total:         55  290  60.0    287    1462

Percentage of the requests served within a certain time (ms)
  50%    287
  66%    289
  75%    291
  80%    292
  90%    296
  95%    299
  98%    325
  99%    352
 100%   1462 (longest request)

Use the terminal.

You can run commands such as running the server through the terminal

php cli -S Address:Port--Filename
Example:
php cli -S 127.0.0.1:9501--index.php

In the same way, your program will be executed


Features list :

  • Http()
  • UdpTcp()
  • WebSocket()
  • Timer()
  • Event()
  • Coroutine() Not ready
  • CoSystem() Not ready
  • CoChannel() Not ready
  • CoWaitGroup() Not ready
  • CoServer() Not ready

By calling any of the above methods, you get access to all related methods.


Setup Server :

require_once '<OpenSwooleApp Folder>/loader.php'; // Require OpenSwooleApp

$server = new OpenswooleApp('127.0.0.1', 9501); // Create Server

Example :

  • Http Server Example

$server = new OpenswooleApp("127.0.0.1", 9501);
    
$server
    ->Http() // Call Http Methode
    ->serverOnStart(function (){
    echo "Server Started";
    }) // = on('Start', <callable>)
    ->end('Hello, OpenSwoole') // = on('request', <callable>) -> end()
    ->start() // Start Server;
  • UdpTcp Server Example

$server = new OpenSwooleApp("127.0.0.1",
    9501, OpenSwoole\Server::SIMPLE_MODE, OpenSwoole\Constant::SOCK_TCP);
    
$server
    ->UdpTcp() // Call UdpTcp Methode
    ->set([
        'worker_num' => 4,
        'daemonize' => true,
        'backlog' => 128,
    ])
    ->on('connect', function() {
        // ...
    })
    ->on('receive', function() {
        // ...
    })
    ->on('close', function() {
        // ...
    });
  • WebSocket Server Example

$server = new OpenSwooleApp("0.0.0.0", 9501);

$server
    ->WebSocket() // Call WebSocket Methode
    ->serverOnStart(function (){
        echo "Server Started";
    })
    ->on('Open', function(OpenSwoole\WebSocket\Server $server, OpenSwoole\Http\Request $request, Timer $timer){
        echo "connection open";

        $timer->tick(1000, function(OpenSwoole\WebSocket\Server $server, OpenSwoole\Http\Request $request){
            $server->push($request->fd, json_encode(["hello", time()]));
        });
    })
    ->on('Message', function(OpenSwoole\WebSocket\Server $server, Frame $frame){
        echo "received message: {$frame->data}\n";
        $server->push($frame->fd, json_encode(["hello", time()]));
    })
    ->on('Close', function(OpenSwoole\WebSocket\Server $serv, int $fd){
        echo "connection close: {$fd}\n";
    })
    ->on('Disconnect', function(OpenSwoole\WebSocket\Server $server, int $fd){
        echo "connection disconnect: {$fd}\n";
    })
    ->start();
  • Event Example

$server
    ->Event() // Call Event Methode
    ->wait();
echo " world\n";
  • Timer Example

$server
    ->Timer() // Call Timer Methode
    ->tick(3000, function () {
        echo "after 3000ms.\n";
    });

Updating ...