未捕获的错误:找不到类'React \ React \ EventLoop \ Factory'

时间:2019-11-08 04:56:14

标签: php reactphp

运行php server.php时出现以下错误:

  

致命错误:未捕获的错误:在C:\ xampp \ htdocs \ socket \ server.php:7中找不到类'React \ React \ EventLoop \ Factory'

我用作曲家抓起作曲家需要反应/插座:^ 1.3 需要autoload.php

use React\Http\Server;

require 'vendor/autoload.php';

$loop = React\EventLoop\Factory::create();
$connector = new React\Socket\Connector($loop);

$connector->connect('127.0.0.1:8080')->then(function (React\Socket\ConnectionInterface $connection) use ($loop) {
    $connection->pipe(new React\Stream\WritableResourceStream(STDOUT, $loop));
    $connection->write("Hello World!\n");
});

$loop->run();

1 个答案:

答案 0 :(得分:0)

嗨,我也有类似的问题,那么首先,请尝试使用以下命令安装事件循环软件包: composer require react / event-loop composer require react / event-loop: ^ 1.1 ,然后尝试使用 composer update 命令。

1。您的作曲家应该在安装所有软件包之后终于喜欢上这个。

{
    "require": {
        "react/http": "^0.8.5",
        "react/mysql": "^0.5.4",
        "nikic/fast-route": "^1.3",
        "react/event-loop": "^1.1"
    },
    "autoload": {
        "psr-4": {
            "App\\": "src/"
        }
    }
}

  1. 您的index.php必须看起来像这样

<?php
require('vendor/autoload.php');

use React\Http\Response;
use React\Http\Server;
use React\MySQL\Factory;

$loop = \React\EventLoop\Factory::create();

$hello = function () {
    return new Response(200, ['Content-type' => 'text/plain'], 'Hello');
};

$server = new Server($hello);
$socket = new \React\Socket\Server('127.0.0.1:8000', $loop);
$server->listen($socket);

echo 'Listening on ' . str_replace('tcp:', 'http:', $socket->getAddress()) . PHP_EOL;
$loop->run();

然后尝试使用php index.php它必须运行。对我来说,它奏效了。 希望对您有帮助!