gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 01:14:53 25 4
gpt4 key购买 nike

下面有一个 on 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();
});

关于php - 如何检索特定的 ReactPHP 套接字错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41618360/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com