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

The connection has no header "User-Agent", so that I have stucking in the readyState 0. #2192

Closed wh131462 closed 5 months ago

wh131462 commented 6 months ago

Is there an existing issue for this?

Description

I tried to establish a connection to my server but kept getting stuck in readyState 0. After numerous unsuccessful attempts, I checked the logs on the backend, which showed that the User-Agent was null. As a workaround, I created a fake one, and it worked. This experience has motivated me to contribute to, or at least understand better, the reasons behind this issue. Perhaps we can collectively improve this library.

ws version

lts

Node.js Version

v20.10.0

System

System: OS: macOS 13.2 CPU: (24) x64 13th Gen Intel(R) Core(TM) i7-13700K Memory: 584.47 MB / 32.00 GB Shell: 5.8.1 - /bin/zsh

Expected result

Having a default User-Agent? Or maybe it's unneccessary.

Actual result

Stucking in readyState 0(CONNECTING), actually the reason is the backend check the Headers to find the User-Agent.

Attachments

No response

lpinca commented 6 months ago

You can add it:

const ws = new WebSocket(url, {
  headers: {
    'user-agent': 'foo'
  }
});
lpinca commented 5 months ago

I'm closing this as answered.