openswoole / ext-openswoole

Programmatic server for PHP with async IO, coroutines and fibers
https://openswoole.com
Apache License 2.0
808 stars 51 forks source link

add native support for windows #334

Closed insinfo closed 1 year ago

insinfo commented 1 year ago

please add native support for Windows, the PHP language has been cross-platform practically forever, and most php developers are working on windows machines

doubaokun commented 1 year ago

There is no plan to support Windows, but you can use Docker.

insinfo commented 1 year ago

@miao-miao Thank you I will test swow https://github.com/swow/swow

insinfo commented 1 year ago

openswoole seems to have a cleaner and simpler API than Swow, Swow seems to be much more detailed at first glance

image

<?php

declare(strict_types=1);

use Swow\Coroutine;
use Swow\CoroutineException;
use Swow\Errno;
use Swow\Http\Protocol\ProtocolException as HttpProtocolException;
use Swow\Http\Status as HttpStatus;
use Swow\Psr7\Client\Client;
use Swow\Psr7\Message\UpgradeType;
use Swow\Psr7\Psr7;
use Swow\Psr7\Server\Server;
use Swow\Socket;
use Swow\SocketException;
use Swow\WebSocket\Opcode as WebSocketOpcode;
use Swow\WebSocket\WebSocket;

require __DIR__ . '/../vendor/autoload.php';

ini_set('memory_limit', '1G');

$host = getenv('SERVER_HOST') ?: '127.0.0.1';
$port = (int) ((($port = getenv('SERVER_PORT')) !== '' && $port !== false) ? $port : 9764);
$backlog = (int) (getenv('SERVER_BACKLOG') ?: Socket::DEFAULT_BACKLOG);

$server = new Server();
$server->bind($host, $port)->listen($backlog);
echo "listen http://127.0.0.1:9764 ";
while (true) {
    try {
        $connection = null;
        $connection = $server->acceptConnection();
        Coroutine::run(static function () use ($connection): void {
            try {
                while (true) {
                    $request = null;
                    try {
                        $request = $connection->recvHttpRequest();
                        switch ($request->getUri()->getPath()) {
                            case '/':
                                $connection->respond('asd');
                                break;
                            case '/greeter':
                                $connection->respond(sprintf('Hello %s', Swow::class));
                                break;
                            case '/echo':
                                $connection->respond($request->getBody());
                                break;   
                            default:
                                $connection->error(HttpStatus::NOT_FOUND);
                        }
                    } catch (HttpProtocolException $exception) {
                        $connection->error($exception->getCode(), $exception->getMessage(), close: true);
                        break;
                    }
                    if (!$connection->shouldKeepAlive()) {
                        break;
                    }
                }
            } catch (Exception) {
                // you can log error here
            } finally {
                $connection->close();
            }
        });
    } catch (SocketException|CoroutineException $exception) {
        if (in_array($exception->getCode(), [Errno::EMFILE, Errno::ENFILE, Errno::ENOMEM], true)) {
            sleep(1);
        } else {
            break;
        }
    }
}