作者 karlet

feat:健壮websocket

... ... @@ -75,21 +75,22 @@ class Websocket
$this->onPing = $onPing;
$this->onPong = $onPong;
Coroutine::create(function () use ($onOpen, $onMessage, $onClose, $onPing, $onPong) {
$this->client = new Client($this->host, $this->port, $this->ssl);
$this->client->set(['timeout' => 5]);
$ret = $this->client->upgrade($this->path);
$client = new Client($this->host, $this->port, $this->ssl);
$client->set(['timeout' => 5]);
$ret = $client->upgrade($this->path);
if ($ret) {
output($this->desc, "连接成功");
$this->client=&$client;
swoole_timer_after(50, function () use ($onOpen) {
if ($onOpen) {
call_user_func($onOpen, $this->client);
call_user_func($onOpen, $client);
}
$this->sendPing();
});
while ($this->client) {
$frame = $this->client->recv($this->recvTimeout);
if (!$frame && $this->client->errCode != 60) {
output($this->desc, "错误数据", $frame);
while ($client) {
$frame = $client->recv($this->recvTimeout);
if (!$frame && $client->errCode != 60) {
output($this->desc,'错误码:' . $client->errCode.",错误数据:", $frame);
break;
}
$this->lastRecvTime = time();
... ... @@ -184,10 +185,4 @@ class Websocket
}
});
}
public function stop():void{
$this->client->close();
swoole_timer_clear($this->timerPing);
swoole_timer_clear($this->timerCheck);
}
}
\ No newline at end of file
... ...