afrad / angular2-websocket

Websocket wrapper for angular2 based on angular-websocket
Apache License 2.0
210 stars 84 forks source link

How can I pass Authorization Header? #89

Open just4give opened 7 years ago

just4give commented 7 years ago

Hi there, Is there any way to pass Authorization header? My backend is secured by OAuth2 and Socket connection failing with 401.

jSchez commented 7 years ago

+1

jSchez commented 7 years ago

For anyone interested, looking on the source, there is a second parameter protocols? which allows sending information. This information will be passed as a header Sec-WebSocket-Protocol with the values needed.

Then, you only need to pass the second parameter in the constructor like this: new $WebSocket( url, [ access_token, refresh_token ] );

NadavK commented 5 years ago

This will cause the connection to fail on "Sent non-empty 'Sec-WebSocket-Protocol' header but no response was received", if the server does not respond back with the same protocols. Details: stackoverflow.com