I require a WebSockets client that runs in client-side Javascript (not node.js) that can send an http header on the initial upgrade GET request. The MDN WebSocket that is available in the browser does not allow sending an http header. The header is required by the server and I have no control over how the server is configured.
Several languages/technologies have WebSockets APIs that allow sending headers, among them: Python, Node-js, .Net (with a hack).
I have no experience with WebAssembly. I'd like to find out if this approach might work, which language/technology might be best and I'm concerned that the resulting WebAssembly files will be very large.
Related SO questions:
Is it possible to implement websockets in Javascript with Streams api? HTTP headers in Websockets client API
I tried using WebAssembly with .Net 7.0, Visual Studio 2022, the 'wasm-tools' workflow and the very helpful blog https://www.meziantou.net/using-dotnet-code-from-javascript-using-webassembly.htm by @meziantou which can get you started with .Net WebAssembly
In short, setting the Header using ClientWebSocket.Options.SetRequestHeader() is not supported and Visual Studio displays a helpful warning telling me that this call is not supported on 'browser'. Needless to say, the code throws an exception when it is executed in the browser.
However, generally it appears to be possible to create a websocket using WebAssembly in .Net and connect.
Based on the developer tools in web browser, the initiator is indicated as "websocket" so I conclude that the functionality is handled by the javascript websocket which as we know does not support sending a header which explains why ClientWebSocket.Options.SetRequestHeader() is not supported in the browser.