WyriHaximus / Ratchet

CakePHP plugin wrapping Ratchet
MIT License
50 stars 11 forks source link

ERR_CONNECTION_TIMED_OUT #138

Open Mark-Lister opened 5 years ago

Mark-Lister commented 5 years ago

Hi, I'm using cakephp 3.6 and php 7.3 I just downloaded this plugin. I've managed to get the server started but I cant seem to connect to it using this code

var conn = new WebSocket('wss://***.***.**.**:8080');
conn.onopen = function(e) {
    console.log("Connection established!");
};

conn.onmessage = function(e) {
    console.log(e.data);
};

I always get the error WebSocket connection to 'wss://***.***.**.**:8080/' failed: Error in connection establishment: net::ERR_CONNECTION_TIMED_OUT

I can connect to this port using telnet I get this

:~# telnet ***.***.**.** 8080
Trying ***.***.**.**...
Connected to ***.***.**.**.
Escape character is '^]'.
^]

HTTP/1.1 500 Internal Server Error
X-Powered-By: Ratchet/0.4.1

Connection closed by foreign host.

This is my configuration in app.php

'WyriHaximus' => [
        'Ratchet' => [
            'internal' => [
                'address' => '0.0.0.0',
                'port' => 8080,
            ],
            'external' => [
                'hostname' => 'myserverip',
                'port' => 8080,
            ],
            'defaults' => [
                'retry_delay_growth' => 0.25,
            ],
            'realm_salt' => 'y[Pw3^o`hLxn`sZkZgtIZ={?B:!Q.8',
            'realm_auth_key_salt' => 'EEA~D`o:ZIIm=hR0bcD$3D+((ti:V;',
            'realms' => [
                'realm1' => [], // Always has to be an array
                'secure' => [
                    'auth' => true,
                    'auth_key' => '5J^e=7ec5g!0j#I4.t%"0Ih2)qp]I)',
                    'max_retries' => 13,
                ],
            ],
        ],
    ]

This is the output of the shell : cake WyriHaximus/Ratchet.websocket start -v

:/var/www/html/team$ sudo ./bin/cake WyriHaximus/Ratchet.websocket start -v
2019-06-03T20:07:53.8281090 debug      [Thruway\Peer\Router 1946] New router created
2019-06-03T20:07:53.8324230 info       [Thruway\Authentication\AuthenticationManager 1946] New client created
2019-06-03T20:07:53.8347350 info       [WyriHaximus\Ratchet\Websocket\InternalClient 1946] New client created
2019-06-03T20:07:53.8350920 info       [WyriHaximus\Ratchet\Websocket\InternalClient 1946] New client created
2019-06-03T20:07:53.8358600 info       [WyriHaximus\Ratchet\Security\AuthorizationManager 1946] New client created
2019-06-03T20:07:53.8418100 info       [WyriHaximus\Ratchet\Security\JWTAuthProvider 1946] New client created
2019-06-03T20:07:53.8423490 info       [Thruway\Peer\Router 1946] Starting router
2019-06-03T20:07:53.8493120 info       [Thruway\RealmManager 1946] Got prehello...
2019-06-03T20:07:53.8496500 debug      [Thruway\RealmManager 1946] Creating new realm 'thruway.auth'
2019-06-03T20:07:53.8528870 debug      [Thruway\RealmManager 1946] Adding realm 'thruway.auth'
2019-06-03T20:07:53.8574150 debug      [Thruway\Authentication\AuthenticationManager 1946] Client onMessage: [Thruway\Message\WelcomeMessage]
2019-06-03T20:07:53.8577300 info       [Thruway\Authentication\AuthenticationManager 1946] We have been welcomed...
2019-06-03T20:07:53.8606620 debug      [Thruway\Authentication\AuthenticationManager 1946] Client onMessage: [Thruway\Message\RegisteredMessage]
2019-06-03T20:07:53.8609350 info       [Thruway\Role\Callee 1946] Setting registration_id for thruway.auth.registermethod (0)
2019-06-03T20:07:53.8628720 info       [Thruway\RealmManager 1946] Got prehello...
2019-06-03T20:07:53.8631410 debug      [Thruway\RealmManager 1946] Creating new realm 'realm1'
2019-06-03T20:07:53.8633950 debug      [Thruway\RealmManager 1946] Adding realm 'realm1'
2019-06-03T20:07:53.8639490 debug      [WyriHaximus\Ratchet\Websocket\InternalClient 1946] Client onMessage: [Thruway\Message\WelcomeMessage]
2019-06-03T20:07:53.8642610 info       [WyriHaximus\Ratchet\Websocket\InternalClient 1946] We have been welcomed...
2019-06-03T20:07:53.8649590 info       [Thruway\RealmManager 1946] Got prehello...
2019-06-03T20:07:53.8652650 debug      [Thruway\RealmManager 1946] Creating new realm 'secure'
2019-06-03T20:07:53.8655030 debug      [Thruway\RealmManager 1946] Adding realm 'secure'
2019-06-03T20:07:53.8660740 debug      [WyriHaximus\Ratchet\Websocket\InternalClient 1946] Client onMessage: [Thruway\Message\WelcomeMessage]
2019-06-03T20:07:53.8663090 info       [WyriHaximus\Ratchet\Websocket\InternalClient 1946] We have been welcomed...
2019-06-03T20:07:53.8667550 info       [Thruway\RealmManager 1946] Got prehello...
2019-06-03T20:07:53.8671960 debug      [WyriHaximus\Ratchet\Security\AuthorizationManager 1946] Client onMessage: [Thruway\Message\WelcomeMessage]
2019-06-03T20:07:53.8674310 info       [WyriHaximus\Ratchet\Security\AuthorizationManager 1946] We have been welcomed...
2019-06-03T20:07:53.8690700 info       [Thruway\Transport\RatchetTransportProvider 1946] Websocket listening on 0.0.0.0:8080
2019-06-03T20:07:53.8705690 info       [Thruway\RealmManager 1946] Got prehello...
2019-06-03T20:07:53.8710720 debug      [WyriHaximus\Ratchet\Security\JWTAuthProvider 1946] Client onMessage: [Thruway\Message\WelcomeMessage]
2019-06-03T20:07:53.8713130 info       [WyriHaximus\Ratchet\Security\JWTAuthProvider 1946] We have been welcomed...
2019-06-03T20:07:53.8717060 debug      [WyriHaximus\Ratchet\Security\JWTAuthProvider 1946] Client onMessage: [Thruway\Message\RegisteredMessage]
2019-06-03T20:07:53.8719500 info       [Thruway\Role\Callee 1946] Setting registration_id for thruway.auth.jwt.onhello (0)
2019-06-03T20:07:53.8724180 debug      [WyriHaximus\Ratchet\Security\JWTAuthProvider 1946] Client onMessage: [Thruway\Message\RegisteredMessage]
2019-06-03T20:07:53.8726580 info       [Thruway\Role\Callee 1946] Setting registration_id for thruway.auth.jwt.onauthenticate (1)
2019-06-03T20:07:53.8747140 debug      [Thruway\Authentication\AuthenticationManager 1946] Client onMessage: [Thruway\Message\InvocationMessage]
2019-06-03T20:07:53.8756390 debug      [WyriHaximus\Ratchet\Security\JWTAuthProvider 1946] Client onMessage: [Thruway\Message\ResultMessage]
2019-06-03T20:07:53.8761820 debug      [WyriHaximus\Ratchet\Security\JWTAuthProvider 1946] Authentication Method Registration Successful: jwt

netstat -npl gives

:~# netstat -npl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      875/mysqld
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      1946/php
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      868/sshd
tcp6       0      0 :::443                  :::*                    LISTEN      1278/apache2
tcp6       0      0 :::80                   :::*                    LISTEN      1278/apache2
tcp6       0      0 :::21                   :::*                    LISTEN      827/vsftpd
tcp6       0      0 :::22                   :::*                    LISTEN      868/sshd

Thanks.

UPDATE When I change the address to the domain name from the external IP in the config and Javascript I get this error instead. WebSocket connection to 'wss://****.*************.com:8080/' failed: WebSocket opening handshake timed out

WyriHaximus commented 5 years ago

So a few things, first off this plugin comes with a helper that sets up the required websocket client for you, inspect the output to get started, it uses https://github.com/WyriHaximus/nodejs-persistent-autobahn . The telnet will do nothing as the server doesn't know what to do with an empty request.

The thing is without errors that says nothing, it could be your firewall or another tool that blocks the connection.

There are no examples, and tbh I'm not sure if there will be as I'm losing interest in maintaining this plugin. But it's a thin layer around https://github.com/voryx/Thruway

Mark-Lister commented 5 years ago

Updated with different error with editing config.

So a few things, first off this plugin comes with a helper that sets up the required websocket client for you, inspect the output to get started, it uses https://github.com/WyriHaximus/nodejs-persistent-autobahn . The telnet will do nothing as the server doesn't know what to do with an empty request.

The thing is without errors that says nothing, it could be your firewall or another tool that blocks the connection.

There are no examples, and tbh I'm not sure if there will be as I'm losing interest in maintaining this plugin. But it's a thin layer around https://github.com/voryx/Thruway

Oh great thanks a lot I got it working with autobahn. I just have one more question, now I can publish and subscribe to events on the client side is there a way I can publish an event from a controller for example when I add a new item to a table so I can get the clients to refresh their data? Thanks again.

WyriHaximus commented 5 years ago

Here is a listener you can build on, you'd still have to get the data to the listener from your controller but this is the recommended way to hook into pubsub and set up RPC's

<?php declare(strict_types=1);

namespace App\Listener;

use Cake\Event\EventListenerInterface;
use React\EventLoop\LoopInterface;
use React\EventLoop\Timer\TimerInterface;
use function React\Promise\resolve;
use Thruway\ClientSession;
use WyriHaximus\Ratchet\Event\OnSessionEndEvent;
use WyriHaximus\Ratchet\Event\OnSessionStartEvent;
useColours;

final class ColoursListener implements EventListenerInterface
{
    const STATE_KEY = 'colour';
    const COLOUR_DEFAULT = '1abc9c';
    const COLOUR_TWITTER = '1da1f2';

    private $colour = self::COLOUR_DEFAULT;

    /**
     * @var LoopInterface
     */
    private $loop;

    /**
     * @var ClientSession
     */
    private $session;

    /**
     * @var TimerInterface
     */
    private $timer;

    /**
     * @var int
     */
    private $timerInterval = 13;

    /**
     * @param LoopInterface $loop
     */
    public function __construct(LoopInterface $loop, Colours $colours, int $timerInterval = 13)
    {
        $this->loop = $loop;
        $this->timerInterval = $timerInterval;
        $colours->subscribe(function (string $colour): void {
            $this->colour = $colour;

            if ($this->session === null) {
                return;
            }

            $this->session->publish('colour', [$colour]);
        });
    }

    public function implementedEvents()
    {
        return [
            OnSessionStartEvent::realmEvent('header')  => 'onSessionStart',
            OnSessionEndEvent::realmEvent('header')    => 'onSessionEnd',
        ];
    }

    public function onSessionStart(OnSessionStartEvent $event): void
    {
        $this->session = $event->getSession();

        $this->session->register('colour', function () {
            return resolve($this->colour);
        });
        $this->timer = $this->loop->addPeriodicTimer($this->timerInterval, function (): void {
            $this->session->publish('colour', [$this->colour]);
        });
    }

    public function onSessionEnd(OnSessionEndEvent $event): void
    {
        $this->session = null;
        $this->loop->cancelTimer($this->timer);
    }
}