'OPEN', 1 => 'CLOSE', 2 => 'PING', 3 => 'PONG', 4 => 'MESSAGE', 5 => 'UPGRADE', 6 => 'NOOP', ]; /** * Engine.io packet types. */ public static $engineTypes = [ 0 => 'CONNECT', 1 => 'DISCONNECT', 2 => 'EVENT', 3 => 'ACK', 4 => 'ERROR', 5 => 'BINARY_EVENT', 6 => 'BINARY_ACK', ]; /** * Get socket packet type of a raw payload. * * @param string $packet * * @return int|null */ public static function getSocketType(string $packet) { $type = $packet[0] ?? null; if (!array_key_exists($type, static::$socketTypes)) { return null; } return (int)$type; } /** * Get data packet from a raw payload. * * @param string $packet * * @return array|null */ public static function getPayload(string $packet) { $packet = trim($packet); $start = strpos($packet, '['); if ($start === false || substr($packet, -1) !== ']') { return null; } $data = substr($packet, $start, strlen($packet) - $start); $data = json_decode($data, true); if (is_null($data)) { return null; } return [ 'event' => $data[0], 'data' => $data[1] ?? null, ]; } /** * Return if a socket packet belongs to specific type. * * @param $packet * @param string $typeName * * @return bool */ public static function isSocketType($packet, string $typeName) { $type = array_search(strtoupper($typeName), static::$socketTypes); if ($type === false) { return false; } return static::getSocketType($packet) === $type; } }