I'm sorry that I am bad news ... but - as mentioned in the question you are referring to, and as you can learn from the standard Websocket API (this is not an external library, this is what comes with the browser) ... you You cannot set your own headers for network connections.
The constructor of WebSocket (url, protocols) takes one or two arguments. The first url argument specifies the URL to connect to. Secondly, the protocols, if any, are either a string or an array of strings .... Each row in the array is a subprotocol name. A connection will be established only if the server reports that it has selected one of these routines ....
But all is not lost.
Since this is YOUR web server, you have options:
I'm sure OAuth2 uses the token as a parameter to request GET or POST and NOT as a custom header. This means that (perhaps) you can pass the token as part of the connection string, i.e.:
websocket = new WebSocket('wss://my.server.com/?access_token=secret_acess_token');
Passing a session token as such may not be ideal, but it may pose a security risk ... so I would go with the second options here:
New connections to web directories (unless my browsers are special) are initiated with the same cookies with which the main connection was established, which means that all cookies and session data from the Http level are accessible to the web memory level. ...
This way you can set a unique cookie - or, even better (assuming that your http and websocket share the same code base and work well together), set the authentication token in the session store on the server side - and use these data to authenticate the connection or refuse it.
Since I'm not a Python expert, here is a small demo using Ruby Plesi Structure (I'm the author):
require 'plezi' class DemoCtrl
visit: http: // loaclhost: 3000 /
to try to start the website, open the web inspector and run the following script in the console:
ws = new WebSocket("ws://localhost:3000/"); ws.onopen = function(e) { console.log("open"); }; ws.onmessage = function(e) { console.log(e.data);}; ws.send("Go Bears");
It must be FAIL, because we have not authenticated yet ...
visit http: // loaclhost: 3000 / login and try again.
Now it should work.
Try http: // loaclhost: 3000 / logout if you like it.
source share