我正在使用 WebSocket 开发一个 angular2 应用程序。我想使用基本身份验证打开 Api 连接。但是 WebSocket 不支持 header 。我不知道如何使用我的 Api 添加我的基本身份验证。
let websocket = new WebSocket("ws://host:port/api/homepage?v=1");
this.websocket.onopen = (evt) => {
this.websocket.send("Hello World");
};
以上代码用于打开WebSocket 连接。我在此处添加基本身份验证。
我试着喜欢这个
// this.websocket.headers("Authorization", "Basic " + btoa(username + ":" + password));
但是会报错
No header method in websocket
最佳答案
试试这个
let websocket = new WebSocket("ws://username:password@host.com:port/api/homepage?v=1");
this.websocket.onopen = (evt) => {
this.websocket.send("Hello World");
};
关于angular - 如何在 websocket 中添加 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47414790/