- marked as enhancement
- marked as minor
Event callback cannot handle $fd param successful
Issue #70
new
<?php
$host = '0.0.0.0';
$port = 50051;
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($sock, $host, $port);
socket_listen($sock);
socket_set_nonblock($sock);
$eventBase = new EventBase();
$event = new Event($eventBase, $sock, EVENT::READ|EVENT::PERSIST, function($sock) {
try {
if(($conn = socket_accept($sock)) != false){
$buf = socket_read($conn, 1024);
$msg = 'hello '. $buf;
socket_write($conn, $msg);
socket_close($conn);
}
}catch (Throwable $e) {
echo $e->getMessage() . PHP_EOL;
}
}, $sock);
$event->add();
$eventBase->loop();
this will warning:
socket_accept(): Argument #1 ($socket) must be of type Socket, int given
but, this case can be successful:
$event = new Event($eventBase, $sock, EVENT::READ|EVENT::PERSIST, function() use ($sock){
...
});
event version:
event
Event support => enabled
Sockets support => enabled
Debug support => disabled
Extra functionality support including HTTP, DNS, and RPC => enabled
OpenSSL support => enabled
Thread safety support => disabled
Extension version => 3.0.6
libevent2 headers version => 2.1.12-stable
php version: 8.1.3
Comments (1)
-
repo owner - Log in to comment
This is not a bug because the callback has never accepted
Socket
, onlyint
or a stream resource (despite of the fact that the docs state that it may be a Socket), and there was no promise that it will be exactlySocket
even if the constructor’s first argument is aSocket
. However, I agree that this needs to be improved.