如何检索特定的 ReactPHP 套接字错误?

How to retrieve specific ReactPHP socket error?

以下有一个错误事件。如何确定具体的错误?

<?php
$loop = Factory::create();
$socket = new React\Socket\Server($loop);
$socket->on('connection', function (\React\Socket\ConnectionInterface $stream){

    $stream->on('data', function($rsp) {
        echo('on data');
    });

    $stream->on('close', function($conn) {
        echo('on close');
    });

    $stream->on('error', function($conn) use ($stream) {
        echo('on error');
        // How do I determine the specific error?
        $stream->close();
    });

    echo("on connect");
});

$socket->listen('0.0.0.0',1337);
$loop->run();

查看 ConnectionInterface 的实现,React\Socket\Connection,它扩展了 React\Stream\Stream,它使用 emit()(这将触发向 on 注册的回调): https://github.com/reactphp/stream/blob/c3647ea3d338ebc7332b1a29959f305e62cf2136/src/Stream.php#L61

$that = $this;
$this->buffer->on('error', function ($error) use ($that) {
    $that->emit('error', array($error, $that));
    $that->close();
});

因此,该函数的第一个参数是错误,第二个参数是 $stream:

$stream->on('error', function($error, $stream) {
    echo "an exception happened: $error";
    // $error will be an instance of Throwable then
    $stream->close();
});