Fixed bug that socketio-server cannot work for swow engine. (#6953)

* Fixed bug that socketio-server cannot work for swow engine.

* Update CHANGELOG-3.1.md
This commit is contained in:
李铭昕 2024-07-17 18:31:09 +08:00 committed by GitHub
parent d01e384836
commit f14128b3a4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 8 deletions

View File

@ -3,6 +3,7 @@
## Fixed ## Fixed
- [#6949](https://github.com/hyperf/hyperf/pull/6949) Fixed bug that restart failed when don't have `.env`. - [#6949](https://github.com/hyperf/hyperf/pull/6949) Fixed bug that restart failed when don't have `.env`.
- [#6953](https://github.com/hyperf/hyperf/pull/6953) Fixed bug that socketio-server cannot work for swow engine.
## Optimized ## Optimized

View File

@ -108,26 +108,28 @@ class SocketIO implements OnMessageInterface, OnOpenInterface, OnCloseInterface
public function onMessage($server, $frame): void public function onMessage($server, $frame): void
{ {
$response = (new Response($server))->init($frame); $response = (new Response($server))->init($frame);
if ($frame->opcode == Opcode::PING) { $frame = Frame::from($frame);
$data = (string) $frame->getPayloadData();
if ($frame->getOpcode() == Opcode::PING) {
$response->push(new Frame(opcode: Opcode::PONG)); $response->push(new Frame(opcode: Opcode::PONG));
return; return;
} }
if ($frame->data[0] === Engine::PING) { if ($data[0] === Engine::PING) {
$this->renewInAllNamespaces($frame->fd); $this->renewInAllNamespaces($response->getFd());
$response->push(new Frame(payloadData: Engine::PONG)); $response->push(new Frame(payloadData: Engine::PONG));
return; return;
} }
if ($frame->data[0] === Engine::CLOSE) { if ($data[0] === Engine::CLOSE) {
$response->close(); $response->close();
return; return;
} }
if ($frame->data[0] !== Engine::MESSAGE) { if ($data[0] !== Engine::MESSAGE) {
$this->stdoutLogger->error("EngineIO event type {$frame->data[0]} not supported"); $this->stdoutLogger->error("EngineIO event type {$data[0]} not supported");
return; return;
} }
$packet = $this->decoder->decode($frame->data); $packet = $this->decoder->decode($data);
switch ($packet->type) { switch ($packet->type) {
case Packet::OPEN: // client open case Packet::OPEN: // client open
$responsePacket = Packet::create([ $responsePacket = Packet::create([
@ -152,7 +154,7 @@ class SocketIO implements OnMessageInterface, OnOpenInterface, OnCloseInterface
$this->sender->pushFrame($response->getFd(), new Frame(payloadData: Engine::MESSAGE . $this->encoder->encode($responsePacket))); $this->sender->pushFrame($response->getFd(), new Frame(payloadData: Engine::MESSAGE . $this->encoder->encode($responsePacket)));
}; };
} }
$this->dispatch($frame->fd, $packet->nsp, ...$packet->data); $this->dispatch($response->getFd(), $packet->nsp, ...$packet->data);
break; break;
case Packet::ACK: // server ack case Packet::ACK: // server ack
$ackId = $packet->id; $ackId = $packet->id;