Files
whatsapp/test_sse_complete.php
T
2026-01-27 14:51:41 -05:00

116 lines
3.9 KiB
PHP
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env php
<?php
/**
* Diagnóstico Completo de SSE
* Prueba la conexión SSE y muestra todos los eventos recibidos
*/
echo "═══════════════════════════════════════════════════════\n";
echo " 🔍 DIAGNÓSTICO COMPLETO SSE\n";
echo "═══════════════════════════════════════════════════════\n\n";
// Configuración
$baseUrl = 'http://localhost:8000';
$token = 'demo_token';
$sseUrl = "{$baseUrl}/api/sse_events.php?token={$token}";
echo "📍 URL SSE: {$sseUrl}\n\n";
// Test 1: Verificar que el endpoint responde
echo "═══ Test 1: Verificar Headers ═══\n";
$ch = curl_init($sseUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
curl_setopt($ch, CURLOPT_NOBODY, true);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
echo "HTTP Code: {$httpCode}\n";
if (strpos($response, 'Content-Type: text/event-stream') !== false) {
echo "✅ Headers SSE correctos\n";
} else {
echo "❌ Headers SSE incorrectos\n";
echo "Headers recibidos:\n{$response}\n";
}
echo "\n═══ Test 2: Conectar y Escuchar Eventos ═══\n";
echo "Esperando eventos (Ctrl+C para cancelar)...\n\n";
// Test 2: Conectar y leer stream
$ch = curl_init($sseUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) {
static $buffer = '';
static $eventCount = 0;
$buffer .= $data;
// Procesar líneas completas
while (($pos = strpos($buffer, "\n")) !== false) {
$line = substr($buffer, 0, $pos);
$buffer = substr($buffer, $pos + 1);
// Parsear línea SSE
if (empty(trim($line))) {
continue;
}
if (strpos($line, 'event:') === 0) {
$eventType = trim(substr($line, 6));
$eventCount++;
echo "[{$eventCount}] 📨 Evento: {$eventType}\n";
} elseif (strpos($line, 'data:') === 0) {
$eventData = trim(substr($line, 5));
$decoded = json_decode($eventData, true);
if ($decoded) {
echo " 📦 Data: " . json_encode($decoded, JSON_PRETTY_PRINT) . "\n";
} else {
echo " 📦 Data: {$eventData}\n";
}
} elseif (strpos($line, 'id:') === 0) {
$eventId = trim(substr($line, 3));
echo " 🆔 ID: {$eventId}\n";
} elseif (strpos($line, 'retry:') === 0) {
$retry = trim(substr($line, 6));
echo " 🔄 Retry: {$retry}ms\n";
} else {
echo " {$line}\n";
}
echo "\n";
}
return strlen($data);
});
echo "Conectando...\n";
$startTime = microtime(true);
$result = curl_exec($ch);
if ($result === false) {
$error = curl_error($ch);
$errno = curl_errno($ch);
echo "\n❌ Error de conexión:\n";
echo " Código: {$errno}\n";
echo " Mensaje: {$error}\n";
} else {
$elapsed = microtime(true) - $startTime;
echo "\n✅ Conexión cerrada después de {$elapsed}s\n";
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo "HTTP Code final: {$httpCode}\n";
curl_close($ch);
echo "\n═══════════════════════════════════════════════════════\n";
echo " ✅ Diagnóstico completado\n";
echo "═══════════════════════════════════════════════════════\n";