websockets / ws

Simple to use, blazing fast and thoroughly tested WebSocket client and server for Node.js
MIT License
21.34k stars 2.3k forks source link

Websocket opens randomly not everytime. #2211

Closed InstaRik closed 3 months ago

InstaRik commented 3 months ago

Is there an existing issue for this?

Description

Hi, I have an websocket server running in Java on a minecraft server. The websocket server is using a letsencrypt certificate. Before I started using the websocket without ssl/tls it didn't randomly connect, it always was connecting, now I am switching to Secure Websockets because of yes the security. Now when I start my websocket in nodejs (its in an discord bot). It won't connect every time. I need to restart the bot a couple of times before it succesfully connects to the secure websocket. What I find strange is when i CTRL C (SIGINT) the websocket server detects a new connection. And after 60s it was closed because of the lost connection detection.

ws version

^8.11.0

Node.js Version

v20.10.0

System

System: OS: Windows 11 10.0.22631 CPU: (12) x64 AMD Ryzen 5 5600 6-Core Processor Memory: 6.46 GB / 15.93 GB

Expected result

That the socket always connects not just randomly.

Actual result

When starting the discord bot (nodejs) the socket stays on CONNECTING STATE 0. Sometimes it goes through to state 1 connected. And upgrades to websocket secure.

Attachments

No response

lpinca commented 3 months ago

Please write a small test case using only ws to reproduce the issue. The provided info is not very useful.

InstaRik commented 3 months ago
const { WebSocket } = require('ws');
const fs = require('fs');

socket = new WebSocket("wss://domain:25566", {
    cert: fs.readFileSync('certs/wss/fullchain.pem'),
    key: fs.readFileSync('certs/wss/privkey.pem'),
    rejectUnauthorized: false,
    handshakeTimeout: 10000
  });

socket.on('open', () => {
    console.log("[LOG Socket] Socket open!");
})
socket._req.on('socket', function (socket) {
    socket.on('data', function (chunk) {
      process.stdout.write(chunk.toString());
    });
  });
lpinca commented 3 months ago

That connects every time for me. It's like this example https://github.com/websockets/ws/blob/master/examples/ssl.js. I can't reproduce the issue.

InstaRik commented 3 months ago

Ow wait is this maybe because im using let instead of const?

lpinca commented 3 months ago

I'm closing this due to inactivity.