打开Web Socket时,我被要求键入我的登录名和密码,但此时我已经登录。如何在自动打开Web套接字时让我的应用程序通过凭据?我只使用基本身份验证,但我找不到任何信息放置授权标头(或任何不同的解决方案我的问题)。
这会在打开Web Socket时弹出(基本登录形式但是在波兰语中):
打开网络套接字的代码:
initializeWebSocketConnection(lobbyName: string): void {
const ws = new SockJS(this.addressStorage.apiAddress + '/socket');
this.stompClient = Stomp.over(ws);
// this.stompClient.debug = null;
const that = this;
this.stompClient.connect({}, function () {
that.stompClient.subscribe('/lobby/' + lobbyName, (message) => {
if (message.body) {
console.log('socket');
}
});
});
}
编辑:我添加标题到connect()
函数后,我仍然需要通过弹出窗体登录。我错过了什么或做错了吗?
改变了代码:
initializeWebSocketConnection(lobbyName: string): void {
const ws = new SockJS(this.addressStorage.apiAddress + '/socket');
this.stompClient = Stomp.over(ws);
// this.stompClient.debug = null;
const that = this;
const headers = {
'authorization': this.authManager.basicToken
};
this.stompClient.connect(headers, function () {
that.stompClient.subscribe('/lobby/' + lobbyName, (message) => {
if (message.body) {
console.log('socket');
}
});
});
}
connect()函数的第一个参数是头对象。您可以在中添加授权。
https://stomp-js.github.io/stomp-websocket/codo/extra/docs-src/Usage.md.html