Join 4,000+ others and never miss out on new tips, tutorials, and more.
4.x is outdated, please check the latest version 22.x
Latest version:
pecl install openswoole-22.1.2
<?php Swoole\WebSocket\Server::unpack(string $data): Swoole\WebSocket\Frame|false
The data/message content you want to unpack. Parses the WebSocket data frame content.
If success, it returns Swoole\WebSocket\Frame
, otherwise it returns false
Unpack data from a WebSocket Frame.
If the parsing fails, then false
will be returned, otherwise you will receive a Swoole\WebSocket\Frame
object with unpacked data.
You can set these OpCodes
WEBSOCKET_OPCODE_TEXT
= 1: String dataWEBSOCKET_OPCODE_BINARY
= 2: Binary dataWEBSOCKET_OPCODE_PING
= 3: Ping packet<?php
// Server setup...
$server->on('Receive', function($server, $fd, $threadId, $data)
{
$dataFrame = Swoole\WebSocket\Server::unpack($data);
if($dataFrame === false)
{
echo "Data failed to parse\n";
$server->send($fd, "Parsing error with: $data\n\n");
}
echo "Data successfully parsed and received:\n";
$server->send($fd, $dataFrame->data . "\n\n");
});
// ...