Skip to content
This repository has been archived by the owner on Oct 23, 2019. It is now read-only.

How to configure WSS working with HTTPS? #35

Open
chantrea opened this issue Nov 22, 2018 · 9 comments
Open

How to configure WSS working with HTTPS? #35

chantrea opened this issue Nov 22, 2018 · 9 comments

Comments

@chantrea
Copy link

Hi here are my steps
My Apache is 2.4.34 on Centos server
in my route
$socket->route('/myclass', new MyClass(), ['*']);
<VirtualHost *:443>
//All Certificates and domain is working fine here.
SSLProxyEngine On
ProxyPreserveHost On
ProxyRequests Off
ProxyPass /wss/ ws://dev.domain.com:2000/ retry=0 keepalive=On
ProxyPassReverse /wss/ ws://dev.domain.com:2000/ retry=0

In client JavaScript Code
pace.min.js:2 WebSocket connection to 'wss://dev.domain.com/wss/myclass' failed: Error during WebSocket handshake: Unexpected response code: 503

And It is working find in localhost with ws://localhost:2000/myclass
Without HTTPS

@tabuna
Copy link
Owner

tabuna commented Nov 22, 2018

Hi @chantrea

It duplicates #12 please note that you answered and gave instructions
You must ensure proxying of requests through your main server (in this case, apache)

Example:

<VirtualHost *:443>
    ServerName website.com

    RewriteEngine On

    # When Upgrade:websocket header is present, redirect to ws
    # Using NC flag (case-insensitive) as some browsers will pass Websocket
    RewriteCond %{HTTP:Upgrade} =websocket [NC]
    RewriteRule ^/ws/(.*)    wss://localhost:8888/ws/$1 [P,L]

    # All other requests go to http
    ProxyPass "/" "http://localhost:8888/"

You can google information, for example on request: apache websocket proxy https

@chantrea
Copy link
Author

I think I also did this way but still have no luck.

@mabuak
Copy link

mabuak commented Nov 22, 2018

Hi, this is my setting when used this library,

# This is for websocket requests
ProxyPass /ws/ ws://imessage.loc:8080/
ProxyPassReverse /ws/ ws://imessage.loc:8080/

RewriteEngine on
RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC]
RewriteCond %{HTTP:CONNECTION} ^Upgrade$ [NC]
RewriteRule .* ws://imessage.loc:8080%{REQUEST_URI} [P]

You can try that setup.. and don't forget to added :

ProxyPreserveHost On

@chantrea
Copy link
Author

chantrea commented Nov 22, 2018

Thank you for your response. I already follow your instruction.

<VirtualHost *:443>
        SSLProxyEngine On
        ProxyPreserveHost On
        ProxyRequests Off
        ProxyPass /wss/ ws://dev.mydomain.com:2000/
        ProxyPassReverse /wss/ ws://dev.mydomain.com:2000/
        RewriteEngine on
        RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC]
        RewriteCond %{HTTP:CONNECTION} ^Upgrade$ [NC]
        RewriteRule .* ws://dev.mydomain.com:2000%{REQUEST_URI} [P]
</VirtualHost>

in my route
$socket->route('/myclass', new MyClass(), ['*']);
And here is the JavaScript Code

class Ws {
        get newClientPromise() {
	    return new Promise((resolve, reject) => {
		$.post("/api/getconfig", {_token: $("#laraveltoken").val(), configname: 'socket.host'}, function(host){
		let wsClient = new WebSocket(host+"/myclass");
 // URL wss://dev.mydomain.com/wss/myclass
		wsClient.onopen = () => {
	             console.log("connected");
	             resolve(wsClient);
               };
	 wsClient.onerror = error => reject(error);
		    });	
	    });
	  }
	  get clientPromise() {
	    if (!this.promise) {
	      this.promise = this.newClientPromise
	    }
	    return this.promise;
	  }
}

window.wsSingleton.clientPromise..then( wsClient =>{wsClient.send('UserID:{{Auth::User()->id}}');})
			         .catch( error => alert(error) );

$.post("/api/getconfig", {_token: $("#laraveltoken").val(), configname: 'socket.host'}, function(host){
  let socket = new WebSocket(host+"/myclass");
      socket.onmessage = function(event) {
       let data = event.data.split("UserID:");
      if(typeof data[1] !== 'undefined')
      {
         if(data[1] == "{{Auth::User()->id}}")
          {
             //do something
           }
         }
     };
});

Error is

pace.min.js:2 WebSocket connection to 'wss://dev.mydomain.com/wss/myclass' failed: Error during WebSocket handshake: Unexpected response code: 503

@mabuak
Copy link

mabuak commented Nov 23, 2018

ProxyRequests Off ProxyPass /wss/ ws://dev.mydomain.com:2000/ ProxyPassReverse /wss/ ws://dev.mydomain.com:2000/ RewriteEngine on RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC] RewriteCond %{HTTP:CONNECTION} ^Upgrade$ [NC] RewriteRule .* ws://dev.mydomain.com:2000%{REQUEST_URI} [P]

Is you'r already installed the ssl certificated?

and installed this packages?

sudo a2enmod proxy && sudo a2enmod proxy_http && sudo service apache2 restart

@chantrea
Copy link
Author

chantrea commented Nov 23, 2018 via email

@chantrea
Copy link
Author

Anyway i am using AWS linux 2 not ubuntu. It is on the top of CentOS

@chantrea
Copy link
Author

Come on guy I tried everythings but it is not working like you all said. Please guy me step by step.

@chantrea chantrea reopened this Nov 27, 2018
@ramixxx
Copy link

ramixxx commented Jun 13, 2019

Not working on my side too. Does anybody resolved connection with wss?

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

No branches or pull requests

4 participants