Hello,
I have a websocket server litening on port 4321 that is using Let's encrypt certificates to broadcast every messages it receives.
When my client connects to the server on this way:
ws://example.org:4321
it runs, but when I'm using this syntax:
wss://example.org:4321
I try to use WSS instead of WS
I have this error message:
{ Error: write EPROTO 101057795:error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol:openssl\ssl\s23_clnt.c:827:
at _errnoException (util.js:1003:13)
at WriteWrap.afterWrite [as oncomplete] (net.js:852:14) errno: 'EPROTO', code: 'EPROTO', syscall: 'write' }
version: 5.0.0
Node.js version(s): 9.6.1
OS version(s): Windows 10 on my computer, Ubuntu 16.04 on the server
Here is the code of the server:
const https = require('https');
const fs = require('fs');
const WebSocket = require('ws');
const server = https.createServer({
cert: fs.readFileSync('/etc/letsencrypt/live/xxx.xxx/fullchain.pem'),
key: fs.readFileSync('/etc/letsencrypt/live/xxx.xxx/privkey.pem')
});
const serveur = new WebSocket.Server(
{
server: server,
port: 4321
}
);
serveur.on('connection', function connection(ws) {
console.log('connection');
ws.on('message', function incoming(data) {
// Broadcast to everyone else.
serveur.clients.forEach(function each(client) {
if (client !== ws && client.readyState === WebSocket.OPEN) {
client.send(data);
}
});
});
});
And the client:
const WebSocket = require('ws');
const ws = new WebSocket('wss://example.org:4321');
var cpt = 0;
ws.on('open', function open() {
var inter = setInterval(() => {
cpt++;
var message = 'test message ' + cpt;
console.log("Client send: " + message);
ws.send(message);
}, 1500);
});
If you pass the port option to the WebSocket.Server constructor an internal plain HTTP server is created and started on the specified port.
Don't add the port option and instead call server.listen() on the created HTTPS server:
const server = https.createServer({
cert: fs.readFileSync('/etc/letsencrypt/live/xxx.xxx/fullchain.pem'),
key: fs.readFileSync('/etc/letsencrypt/live/xxx.xxx/privkey.pem')
});
const wss = new WebSocket.Server({ server });
server.listen(4321);
It solved the problem!
Thank you very much.
Most helpful comment
If you pass the
portoption to theWebSocket.Serverconstructor an internal plain HTTP server is created and started on the specified port.Don't add the
portoption and instead callserver.listen()on the created HTTPS server: