felangel / web_socket_client

A simple WebSocket client for Dart which includes automatic reconnection logic.
https://pub.dev/packages/web_socket_client
MIT License
150 stars 32 forks source link

fix: headers not included in request on web platform #48

Closed wkok closed 4 months ago

wkok commented 4 months ago

Description

When specifying headers in the connect request, they are not included in the server request when the platform is web.

(It works on linux)

Steps To Reproduce

v0.1.1

import 'package:web_socket_client/web_socket_client.dart';

WebSocket connect(url) {
  return WebSocket(url, headers: {"test": "header"});
}

Expected Behavior

Headers to be included in the network request.

Screenshots

It works on linux:

Screenshot from 2024-04-27 07-12-56

but not on web (chrome):

Screenshot from 2024-04-27 07-25-31

felangel commented 4 months ago

Hi @wkok 👋 Thanks for opening an issue!

This is working as intended because afaik it's not possible to set headers as part of the WebSocket MDN Specification.

Also see https://stackoverflow.com/questions/4361173/http-headers-in-websockets-client-api.

Hope that helps 👍

wkok commented 4 months ago

Thanks for the links, I've opted for going the protocols route and sending the data in the Sec-WebSocket-Protocol headers as suggested here