作者 karlet

feat:健壮websocket

@@ -75,21 +75,22 @@ class Websocket @@ -75,21 +75,22 @@ class Websocket
75 $this->onPing = $onPing; 75 $this->onPing = $onPing;
76 $this->onPong = $onPong; 76 $this->onPong = $onPong;
77 Coroutine::create(function () use ($onOpen, $onMessage, $onClose, $onPing, $onPong) { 77 Coroutine::create(function () use ($onOpen, $onMessage, $onClose, $onPing, $onPong) {
78 - $this->client = new Client($this->host, $this->port, $this->ssl);  
79 - $this->client->set(['timeout' => 5]);  
80 - $ret = $this->client->upgrade($this->path); 78 + $client = new Client($this->host, $this->port, $this->ssl);
  79 + $client->set(['timeout' => 5]);
  80 + $ret = $client->upgrade($this->path);
81 if ($ret) { 81 if ($ret) {
82 output($this->desc, "连接成功"); 82 output($this->desc, "连接成功");
  83 + $this->client=&$client;
83 swoole_timer_after(50, function () use ($onOpen) { 84 swoole_timer_after(50, function () use ($onOpen) {
84 if ($onOpen) { 85 if ($onOpen) {
85 - call_user_func($onOpen, $this->client); 86 + call_user_func($onOpen, $client);
86 } 87 }
87 $this->sendPing(); 88 $this->sendPing();
88 }); 89 });
89 - while ($this->client) {  
90 - $frame = $this->client->recv($this->recvTimeout);  
91 - if (!$frame && $this->client->errCode != 60) {  
92 - output($this->desc, "错误数据", $frame); 90 + while ($client) {
  91 + $frame = $client->recv($this->recvTimeout);
  92 + if (!$frame && $client->errCode != 60) {
  93 + output($this->desc,'错误码:' . $client->errCode.",错误数据:", $frame);
93 break; 94 break;
94 } 95 }
95 $this->lastRecvTime = time(); 96 $this->lastRecvTime = time();
@@ -184,10 +185,4 @@ class Websocket @@ -184,10 +185,4 @@ class Websocket
184 } 185 }
185 }); 186 });
186 } 187 }
187 -  
188 - public function stop():void{  
189 - $this->client->close();  
190 - swoole_timer_clear($this->timerPing);  
191 - swoole_timer_clear($this->timerCheck);  
192 - }  
193 } 188 }