Please answer these questions before submitting your issue. Thanks!
public function onRequest(\swoole_http_request $request, \swoole_http_response $response)
{
$data = [
'message' => $request->post['info']
];
foreach ($this->webSocketServ->connections as $fd) {
try {
$this->webSocketServ->push($fd, json_encode($data));
} catch (Exception $e) {
}
}
}
客户端关闭后应该就开始触发 close ,client 已经从 $server->connections 中移除才对 ,为什么会报这个错误 ?
PHP Warning: Swoole\WebSocket\Server::push(): the connected client of connection[49] is not a websocket client or closed.
php --ri swoole)?[root@localhost 09]# php --ri swoole
swoole
swoole support => enabled
Version => 4.2.9
Author => Swoole Group[email: [email protected]]
coroutine => enabled
epoll => enabled
eventfd => enabled
signalfd => enabled
cpu_affinity => enabled
spinlock => enabled
rwlock => enabled
openssl => OpenSSL 1.0.2k-fips 26 Jan 2017
pcre => enabled
zlib => enabled
mutex_timedlock => enabled
pthread_barrier => enabled
futex => enabled
async_redis => enabled
Directive => Local Value => Master Value
swoole.enable_coroutine => On => On
swoole.aio_thread_num => 2 => 2
swoole.display_errors => On => On
swoole.use_namespace => On => On
swoole.use_shortname => On => On
swoole.fast_serialize => Off => Off
swoole.unixsock_buffer_size => 8388608 => 8388608
[root@localhost 09]# uname -a
Linux localhost.localdomain 3.10.0-862.el7.x86_64 #1 SMP Fri Apr 20 16:44:24 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
发送期间客户端可能会断开的, 而你遍历的PHP迭代器是不可能实时更新的, 在发送的时候底层会再次获取连接, 如果发现客户端断开了就会警告, 你可以加个@抑制警告, 这个在群发时没有影响, 可以安心@.
使用WebSocket\Server->isEstablished判断fd是否为可用的websocket连接