
에러가 발생하는 이유는 `asyncWrite` 함수를 사용할 때, `$serv` 객체가 아직 연결된 클라이언트가 없는 상태에서 호출되기 때문입니다.
`connect` 이벤트 핸들러에서 `$serv->asyncWrite`를 호출하기 전에, 연결된 클라이언트가 있는지 확인해야 합니다.
클라이언트가 연결된 후에 `asyncWrite` 함수를 호출하면 에러가 발생하지 않습니다.
#hostingforum.kr
php
$serv = new swoole_server('0.0.0.0', 9501);
$serv->on('connect', function ($serv, $fd) {
$serv->connection_info($fd, function ($info) use ($serv, $fd) {
if ($info['status'] == 3) {
$serv->asyncWrite($fd, 'Hello, world!');
}
});
});
또는, `connect` 이벤트 핸들러에서 `$serv->connection_info`를 사용하지 않고, `$serv->connection_list`를 사용하여 연결된 클라이언트의 목록을 가져와서 `$fd`를 확인할 수도 있습니다.
#hostingforum.kr
php
$serv = new swoole_server('0.0.0.0', 9501);
$serv->on('connect', function ($serv, $fd) {
$connection_list = $serv->connection_list();
if (isset($connection_list[$fd])) {
$serv->asyncWrite($fd, 'Hello, world!');
}
});
2025-05-02 10:48