Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to connect to a websocket

I created a PHP websocket using Ratchet. This Websocket is running on port 8080 of my internal server 10.0.4.160.

I am trying to connect to it from a website that have SSL enabled "aka using https protocol."

When attempting to connect to the websocket form a FireFox browser, I get security issues from the browser because I am mixing between secured/non-secured connection. This is the error that I see in the javascript's console.

SecurityError: The operation is insecure.

To fix the security issue I created a stunnel proxy which allows me to accept the connection on 10.0.4.160:58443 and connect it to port 127.0.0.1:8080. This secure tunnel should allow me to keep my connection to the websocket secured and bypass the browser's security check.

However, every time I attempt to connect to the websocket I get an error

WebSocket connection to 'wss://10.0.4.160:58443/' failed: Error in connection establishment: net::ERR_TIMED_OUT

This is the jQuery script that is used to connect to the websocket

<script>

    $(function(){
        if ("WebSocket" in window)
        {
            var conn = new WebSocket('wss://10.0.4.160:58443');

            conn.onopen = function(e) {
                console.log("Connection established!");
                showMessage('Connected', 0);
            };

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

            conn.onclose = function(e) {
                console.log(e.code);
                console.log(e.reason);
            };              

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

        } else {
            console.log('Your browser does not support Websocket!');
        }

        function showMessage(msg, append){
            if(append){
                $('#status').append('<br>' + msg);
                return;
            }

            $('#status').text(msg);
        }
    });

</script>

Here is my current stunnel configuration

[websockets]
client = yes
accept = 58443
connect = 8080
verify = 2
CAfile = ca-certs.pem
debug = 7
OCSPaia = yes

How can I connect to the websocket from my browser? Thank you

like image 326
Junior Avatar asked Aug 17 '15 18:08

Junior


Video Answer


1 Answers

read more about html5 websockets at http://www.html5rocks.com/en/tutorials/websockets/basics/

Socket.io is a popular web socket library read more at www.socket.io

There is not enough information to diagnose your issue. Please provide a server log.

  • Does your server recognize a connection attempt?
  • Does your server handle the client server handshake?

If your server recognizes a connection attempt then your issue is more than likely the handshake. There is not an issue with your JavaScript.


EXTRA INFORMATION:

another type of connection: (we will call this type 1)

|SERVER(PHP)| <--- CONNECTION --> |SERVER(websocket)|

what you are using: (we will call this type 2)

|BROWSER(client)| <----CONNECTION --> |SERVER(websocket)|

when using type 1 connections as type 2 connections you risk overflowing your main web server which will cause sever performance issues. Web host like: Godaddy, Hostgator.. ect.. will terminate connections that are flooding their servers because they are seen as DDOS attacks.

I dont know what ratchet is, but to keep a server & client connection open PHP will need to stay open & this is NEVER NEVER NEVER EVER EVER a good idea. Look into created a server in languages such as C# or C++.

like image 51
THE AMAZING Avatar answered Sep 21 '22 15:09

THE AMAZING