根据我在基本javascript客户端上阅读和测试的内容,我可以在Angular Client中传递额外的标头,然后在我的NodeJs服务器中使用 socket.handshake 。但是我不确定我缺少什么或做错了什么,因为下面的代码不会创建
async socketInit() {
this.client = io(`${environment.socketUrl}`, {
transportOptions: {
polling: {
extraHeaders: {
Authorization: "Bearer authorization_token_here"
}
}
}
});
await new Promise((res) => {
this.client.on('connect', () => {
this.isConnected = true;
this.socketId = this.client.id;
this.client.on('disconnect', () => this.isConnected = false);
res();
});
})
.catch((e) => console.log(`Socket.io error: ${e.message}`));
}
由于某种原因,这会导致连接失败,正如我在我的调试控制台中所看到的,即socket.io有一个TransportError