Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ws to wss websocket How to run in https #636

Closed
kergdgreat opened this issue Apr 20, 2018 · 5 comments
Closed

ws to wss websocket How to run in https #636

kergdgreat opened this issue Apr 20, 2018 · 5 comments

Comments

@kergdgreat
Copy link

i can't run my ratchet websocket in apache 2.2 ver. with an https access. but without https no issues at all.

this error always occur.
PHP Fatal error: Uncaught exception 'React\Socket\ConnectionException' with message 'Could not bind to tcp://domain:8081: Address already in use' in /var/www/html/src/websocketChat/inc/vendor/react/socket/src/Server.php:29
Stack trace:
#0 /var/www/html/src/websocketChat/inc/vendor/cboden/ratchet/src/Ratchet/Server/IoServer.php(70): React\Socket\Server->listen(8081, 'domain.c...')
#1 /var/www/html/src/websocketChat/inc/server.php(22): Ratchet\Server\IoServer::factory(Object(Ratchet\Http\HttpServer), 8081, 'domain.c...')
#2 /var/www/html/src/websocketChat/inc/bg.php(4): include_once('/var/www/html/s...')
#3 {main}
thrown in /var/www/html/src/websocketChat/inc/vendor/react/socket/src/Server.p

@bmjhversteeg
Copy link

When do you receive this error? How did you configure your code and environment for sockets over tls? The error you provided means there is already a process running that occupies port 8081. You should kill that process first and restart your script for changes to take effect. Without other information, I can't help you further.

@nishanth6
Copy link

nishanth6 commented Oct 7, 2018

Will some one could explain me a bit about WebSocket Connection to the hosted server?

@kergdgreat @bmjhversteeg

I had an issue while connecting WebSocket (socketo.me) to the hosted server

For instance if the domain name is such that http://somelabz.ml/chatroom-php-mysq
How should I set the conn object
var conn = new WebSocket('ws://localhost:8383');

Click here for detailed report

@njasm
Copy link

njasm commented Dec 7, 2018

@kergdgreat take a closer look to what @bmjhversteeg said.

the error your are reporting has nothing to do with websocket connection. you just happen to have some other service already running on port 8081 on the machine that you're tryin to run your websocket server.

@nstechns
Copy link

if you are using laravel library cboden/ratchet for socket then go to app/Console/Commands/WebSocketServer.php Replace handle function with these lines of code and use your certification file path 'local_cert' and 'local_pk'


public function handle()
{

    $loop   = Factory::create();
    $webSock = new SecureServer(
        new Server('0.0.0.0:8090', $loop),
        $loop,
        array(
            'local_cert'        => '/opt/ssl/chamberstock_com.crt', // path to your cert
            'local_pk'          => '/opt/ssl/server.key', // path to your server private key
            'allow_self_signed' => TRUE, // Allow self signed certs (should be false in production)
            'verify_peer' => FALSE
        )
    );
    // Ratchet magic
    $webServer = new IoServer(
        new HttpServer(
            new WsServer(
                new WebSocketController()
            )
        ),
        $webSock
    );
    $loop->run();

}

@joaquin-gs
Copy link

joaquin-gs commented Aug 18, 2021

if you are using laravel library cboden/ratchet for socket then go to app/Console/Commands/WebSocketServer.php Replace handle function with these lines of code and use your certification file path 'local_cert' and 'local_pk'


public function handle()
{

    $loop   = Factory::create();
    $webSock = new SecureServer(
        new Server('0.0.0.0:8090', $loop),
        $loop,
        array(
            'local_cert'        => '/opt/ssl/chamberstock_com.crt', // path to your cert
            'local_pk'          => '/opt/ssl/server.key', // path to your server private key
            'allow_self_signed' => TRUE, // Allow self signed certs (should be false in production)
            'verify_peer' => FALSE
        )
    );
    // Ratchet magic
    $webServer = new IoServer(
        new HttpServer(
            new WsServer(
                new WebSocketController()
            )
        ),
        $webSock
    );
    $loop->run();

}

I implemented nstechns suggestion, but I keep getting this error:
Object of class React\Socket\SecureServer could not be converted to string

at D:\web\app\ssa\vendor\cboden\ratchet\src\Ratchet\Server\IoServer.php:59
55▕ * @return IoServer
56▕ */
57▕ public static function factory(MessageComponentInterface $component, $port = 80, $address = '0.0.0.0') {
58▕ $loop = LoopFactory::create();
➜ 59▕ $socket = new Reactor($address . ':' . $port, $loop);
60▕
61▕ return new static($component, $socket, $loop);
62▕ }
63▕

1 D:\web\app\ssa\app\Console\Commands\WebSocketServer.php:60
Ratchet\Server\IoServer::factory(Object(Ratchet\Http\HttpServer), Object(React\Socket\SecureServer))

2 D:\web\app\ssa\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php:36
App\Console\Commands\WebSocketServer::handle()

Any clue on what am I missing?

Here is the code I am using :

   public function handle()
   {
      $loop = Factory::create();
      $webSock = new Server('0.0.0.0:8443', $loop);
      $socketServer = new SecureServer(
         $webSock, 
         $loop, 
         [	'local_cert' => 'certificate.crt',
            'local_pk' => 'private.key',
            'allow_self_signed' => true, 
            'verify_peer' => false
         ]
      );
      $server = IoServer::factory(new HttpServer(new WsServer(new WebSocketController())), $socketServer);
      $server->run();
   }

OS: Microsoft Windows 10
WAMP server 3.2.0
Laravel 8.0
PHP 7.4
cboden/ratchet version 0.4.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants