walkor / workerman

An asynchronous event driven PHP socket framework. Supports HTTP, Websocket, SSL and other custom protocols.
http://www.workerman.net
MIT License
11.03k stars 2.25k forks source link

Why i can't connect to my SSE (Server-Sent-Events) server? #946

Closed shizzic closed 11 months ago

shizzic commented 11 months ago
use Workerman\Worker;
use Workerman\Protocols\Http\Response;
use Workerman\Protocols\Http\ServerSentEvents;
use Workerman\Connection\TcpConnection;
use \Workerman\Lib\Timer;
require_once '/var/www/www-root/data/www/api.isinda.ru/vendor/autoload.php';

$context = array(
    'ssl' => array(
        'local_cert'  => 'path_to_cert',
        'local_pk'    => 'path_to_pk',
        'verify_peer' => false,
    )
);

$worker = new Worker('http://0.0.0.0:8090', $context);
$worker->name = 'sse';
$worker->count     = 2;
$worker->transport = 'ssl';

$worker->onMessage = function ($connection, $request) {
    if ($request->header('accept') === 'text/event-stream') {
        $connection->send(new Response(200, ['Content-Type' => 'text/event-stream'], "\r\n"));
        $timer_id = Timer::add(2, function () use ($connection, &$timer_id) {
            if ($connection->getStatus() !== TcpConnection::STATUS_ESTABLISHED) {
                Timer::del($timer_id);
                return;
            }
            $connection->send(new ServerSentEvents(['event' => 'message', 'data' => 'hello', 'id' => 1]));
        });
        return;
    }
    $connection->send('ok');
};

$worker->onConnect = function ($connection) {
    $connection->send('connected');
};

$worker->onError = function ($connection) {
    $connection->send('error');
};

Worker::runAll();

That's how i tried to connect

let source = new EventSource("https://domain:8090", {
      withCredentials: true,
    })

So, basically i can't connect to this event server and keep getting this error "GET https://domain:8090/ net::ERR_CONNECTION_TIMED_OUT" with no status in response at all. It's your only code instance with sse implementation i found. It's cross-domain and on the other hand i successfully connected to websocket server on the same domain. There is no issue with paths to certificates (i checked) and no fatal error at all. Log files don't show anything. So i just can't connect.

shizzic commented 11 months ago

And yes, i have a right version of workerman "workerman/workerman": "^4.0",

fuzqing commented 11 months ago

Please remove the code:

$worker->onConnect = function ($connection) { $connection->send('connected'); }; $worker->onError = function ($connection) { $connection->send('error'); };

Then open your browser console and enter the following JS code:

var source = new EventSource('https://domain:8090');
source.addEventListener('message', function (event) {
  var data = event.data;
  console.log(data); // print hello
}, false);
shizzic commented 11 months ago

Nothing changed

fuzqing commented 11 months ago

start.php: image

js code and sse response: image

workerman version: image

shizzic commented 11 months ago

start.php: image

js code and sse response: image

workerman version: image

Thank you, it was my problem with untachhed port, so i changed it on available and it worked.