WebSocket error establishing connection: net :: ERR_CONNECTION_CLOSED

I get this error when I try to establish a wss connection with my server:

Connection to WebSocket with 'wss: // mydomain: 3000 /' failed: connection error: net :: ERR_CONNECTION_CLOSED

Currently, I have set up the apache2 virtual host configuration to listen for requests on ports 443 and 80:

 <VirtualHost *:80> ServerName otherdomainname.co.uk ServerAlias www.otherdomainname.co.uk RewriteEngine On RewriteRule ^/(.*)$ /app/$1 [l,PT] JkMount /* worker2 </VirtualHost> <VirtualHost _default_:443> ServerName otherdomainname.co.uk ServerAlias www.otherdomainname.co.uk RewriteEngine On RewriteRule ^/(.*)$ /app/$1 [l,PT] SSLEngine On SSLCertificateFile /etc/apache2/ssl/apache.crt SSLCertificateKeyFile /etc/apache2/ssl/apache.key <Location /> SSLRequireSSL On SSLVerifyClient optional SSLVerifyDepth 1 SSLOptions +StdEnvVars +StrictRequire </Location> JkMount /* worker2 </VirtualHost> 

As you can see, it uses JkMount to send a Tomcat request, which correctly serves the web page for both HTTP and HTTPS.

When I visit a site using the HTTP protocol on port 80, a WebSocket connection can be made using the ws protocol.

When I visit a site using the HTTPS protocol on port 443, the site is maintained correctly, but the connection to WebSocket is not performed using wss .

I am using the ws node.js module to provide a WebSocket server:

 var WebSocketServer = require('ws').Server , wss = new WebSocketServer({ port: 3000 }), fs = require('fs'); wss.on('connection', function connection(ws) { ws.on('message', function incoming(message) { console.log('received: %s', message); ws.send(message); ws.send('something'); }); 

Why can't I successfully connect to the WebSocket server using the wss protocol through https ?

+8
apache websocket wss
source share
2 answers

The problem was that I did not configure the WebSocket server for https / wss.

Here is a secure version of my insecure WebSocket server using "ws" from node.js.

 var WebSocketServer = require('ws').Server, fs = require('fs'); var cfg = { ssl: true, port: 3000, ssl_key: '/path/to/apache.key', ssl_cert: '/path/to/apache.crt' }; var httpServ = ( cfg.ssl ) ? require('https') : require('http'); var app = null; var processRequest = function( req, res ) { res.writeHead(200); res.end("All glory to WebSockets!\n"); }; if ( cfg.ssl ) { app = httpServ.createServer({ // providing server with SSL key/cert key: fs.readFileSync( cfg.ssl_key ), cert: fs.readFileSync( cfg.ssl_cert ) }, processRequest ).listen( cfg.port ); } else { app = httpServ.createServer( processRequest ).listen( cfg.port ); } var wss = new WebSocketServer( { server: app } ); wss.on('connection', function connection(ws) { ws.on('message', function incoming(message) { console.log('received: %s', message); ws.send(message); }); ws.send('something'); }); 
+9
source share

I had a similar problem, it turned out that I used CloudFlare, which only allows very specific ports.

Thus, the meteor operating on port 3000 was instantly blocked.

Reconfiguring the reverse proxy settings and running Meteor on the allowed port solved my problem.

But in the end, I turned off the sockets on my Meteor deployment. It does not appear to have affected performance. Good luck

0
source share

All Articles