how to add headers in websocket

Viewed 1861

I am developing an angular2 application with WebSocket. I want to open Api connection with basic authentication. But WebSocket does not support headers. I don't know how to add my basic authentication with my Api.

let websocket = new WebSocket("ws://host:port/api/homepage?v=1");
      this.websocket.onopen =  (evt) => {
          this.websocket.send("Hello World");
};

Above code used to open WebSocket connection. Here where I add my basic authentication.

I tried to like this

// this.websocket.headers("Authorization", "Basic " + btoa(username + ":" + password));

But it throws error

No header method in websocket

1 Answers

try this

let websocket = new WebSocket("ws://username:password@host.com:port/api/homepage?v=1");
      this.websocket.onopen =  (evt) => {
          this.websocket.send("Hello World");
};
Related