workerd icon indicating copy to clipboard operation
workerd copied to clipboard

🐛 Bug Report — Runtime APIs `WebSocket` can't connect to remote server when using custom port number

Open tien opened this issue 1 year ago • 1 comments

Behaviour

When connecting to a remote web socket server running on a custom port. Cloudflare worker will try to use port 80/443 instead of the one specified in the URL, resulting in the following error if the url path doesn't exist on port 80/443:

Error: Uncaught TypeError: Failed to establish the WebSocket connection: expected server to reply with HTTP status code 101 (switching protocols), but received 404 instead.

This behaviour only happen with deployed worker, so if you run wrangler dev, local mode will work fine, but once you turn off local mode, the error will happen.

Reproduction steps

  • Create a server with a web socket application listening on port 8182 at path /my-path
  • Also add a normal web application on port 80 & 443 at path /, any other http(s) request to different path will return 404
  • Observe that new WebSocket('wss://thedomain.com:8182/my-path') work correctly everywhere, but fail on a deployed worker, throwing 404 not found

tien avatar Mar 01 '24 12:03 tien

If I put the web socket application at port 80, 443 & 8182 then the socket connection to port 8182 work as expected. So it seems like Cloudflare worker will always go to the default port first and will error if the default port return 404, and then it will go to the port specified in the URL.

tien avatar Mar 01 '24 13:03 tien