0

here's my Ratchet server file

<?php 
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use ChatWebSocket\Application;
require 'Application.php';
require __DIR__ . '/vendor/autoload.php';

$server = IoServer::factory(
    new HttpServer(
        new WsServer(
            new Application()
        )
    ),
    2020
);
$server->run();

And this is Client side using WS Javascript to connect

(function(){
    var conn = new WebSocket('wss://mydomain.net:2020');

    conn.onopen = function(e) {
        console.log("Connection OK!");
    };

    conn.onmessage = function(e) {
        var res = JSON.parse(e.data);
        alert(res);

    };
})();

when i turn off SSL mode (http:// connect) and using ws://mydomain.net:2020 then the server works fine!

And when i turn on SSL mode, connect to server with wss://mydomain.net:2020, go to client web with https:// then i get this error from console

WebSocket connection to 'wss://mydomain.net:2020/' failed: WebSocket opening handshake timed out

i've tried add this setting to my httpd.conf file

ProxyPass /wss2/ ws://mydomain.net:2020/

but i got same error.

how can i fix this error? how to connect websocket with SSL (https://) ?

Ray Lian
  • 9
  • 4
  • I think you are actually aware of this duplicate and tried to use it. But, look closer which URL should be used with wss:// and you will see that it is using the default port of wss:// and not the port where the ws:// server is on. That's because it first connects with https to Apache (default port) which then *internally* forwards the traffic to the ws:// proxy - after stripping the TLS layer. – Steffen Ullrich Jul 01 '17 at 13:42
  • Thank you! But when i turn on ssl i also i replace ws:// to wss:// to try it on https. But failed! – Ray Lian Jul 01 '17 at 14:19
  • To cite myself: *"...look closer ... you will see that it is using the default port of wss://..."*. Thus, don't use `wss://mydomain.net:2020/` as you do but use `wss://mydomain.net/`, i.e. omit the port number so that it uses the default 443 where Apache is listening on and where it is forwarding the non-TLS traffic to `ws://mydomain.net:2020/`. And don't try to use `https://` instead of `wss://` because the first is for normal secure HTTP traffic and the other for secure websockets. – Steffen Ullrich Jul 01 '17 at 14:41

0 Answers0