Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Looking for help to migrate from 2.x to 4.0.0 :( #28

Open
DrLightman opened this issue Jan 3, 2025 · 0 comments
Open

Looking for help to migrate from 2.x to 4.0.0 :( #28

DrLightman opened this issue Jan 3, 2025 · 0 comments

Comments

@DrLightman
Copy link

Hello, I'm dusting off a 2020 PHP 7.4 project - a small game - I made using these libs:

{
    "require": {
        "amphp/websocket-server": "^2",
        "amphp/http-server-router": "^1",
        "amphp/http-server-static-content": "^1",
        "amphp/log": "^1",
        "ext-json": "*"
    },
    "minimum-stability": "RC"
}

My goal would be to make it work with PHP 8.4.1 therefore I updated the composer.json as:

{
    "require": {
        "amphp/websocket-server": "4.0.0",
        "amphp/http-server-router": "2.0.0",
        "amphp/http-server-static-content": "2.0.0",
        "amphp/log": "2.0.0",
        "ext-json": "*"
    },
    "minimum-stability": "RC"
}

Did a composer clearcache, removed vendor completely followed by the update.

Needless to say, the program doesn't even start :(
I must confess I'm not a smart ass regarding event driven php, infact I don't even remember which tutorial I followed to write the boot sequence with all the AMPHP stuff.

The error I'm getting is:

Fatal error: Uncaught Error: Interface "Amp\Websocket\Server\ClientHandler" not found in D:\Siti\game\server-php\start.php:34

I'll leave here a synthetic revisioned version of the boot sequence if anoyone is willing to drop some tips :(

use Amp\Http\Server\HttpServer;
use Amp\Http\Server\Request;
use Amp\Http\Server\Response;
use Amp\Http\Server\Router;
use Amp\Http\Server\Driver\RemoteClient;
use Amp\Http\Server\StaticContent\DocumentRoot;
use Amp\Log\ConsoleFormatter;
use Amp\Log\StreamHandler;
use Amp\Loop;
use Amp\Promise;
use Amp\Socket\Server as SocketServer;
use Amp\Success;
use Amp\Websocket\Client;
use Amp\Websocket\Message;
use Amp\Websocket\Server\ClientHandler;
use Amp\Websocket\Server\Websocket;
use Monolog\Logger;
use function Amp\ByteStream\getStdout;
use function Amp\call;

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

$websocket = new Websocket(new class implements ClientHandler {
    /** @var Websocket */
    private $endpoint;

    public function onStart(Websocket $endpoint) : Promise {
        $this->endpoint = $endpoint;
        return new Success;
    }

    public function onStop(Websocket $endpoint) : Promise {
        global $logger;
        $logger->info(sprintf('Client disconnected?'));
        return new Success; // Simple example, nothing to do.
    }

    /**
     * one shot call
     * @param Request $request
     * @param Response $response
     * @return Promise
     */
    public function handleHandshake(Request $request, Response $response): Promise
    {
        global $CONFIG, $logger;

        if (!\in_array($o = $request->getHeader('origin'), $CONFIG['allowedOrigins'], true)) {
            $logger->warning(sprintf('rejected request due to unknown origin: %s', $o));
            $response->setStatus(403);
        }

        $logger->info(sprintf('Origin = %s', $request->getHeader('origin')));

        return new Success($response);
    }

    /**
     * one shot call
     * @param Client $client
     * @param Request $request
     * @param Response $response
     * @return Promise
     */
    public function handleClient(Client $client, Request $request, Response $response): Promise
    {
        global $logger;

        return call(function () use ($client, $request, $logger): \Generator {

            $logger->info('User connected');

            $client->onClose(function(Client $client, int $code, string $reason) use ($logger) {
                $logger->info(sprintf('Client %s disconnected [code: %s, reason: %s]', $client->getId(), $code, $reason));
            });

            while ($message = yield $client->receive()) {
                echo yield $message->buffer();
                \assert($message instanceof Message);
            }
        });
    }
});

$logHandler = new StreamHandler(getStdout());
$logHandler->setFormatter(new ConsoleFormatter(null, null, false, true));
$logger = new Logger('server');
$logger->pushHandler($logHandler);

Loop::defer(function () use ($websocket, $logger): Promise {
    global $CONFIG;

    $sockets = [];
    foreach ($CONFIG['listen'] as $uri) {
        $sockets[] = SocketServer::listen($uri);
    }

    $router = new Router;
    $router->addRoute('GET', '/', $websocket);

    $server = new HttpServer($sockets, $router, $logger);

    return $server->start();
});

Loop::run();

Thank you and happy new year!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant