如何在打开网络套接字时自动登录
How to automatically log in while opening web socket
在打开 Web Socket 时,我被要求输入我的登录名和密码,但此时我已经登录了。如何让我的应用程序在自动打开 Web 套接字时传递凭据?我只使用基本身份验证,但我找不到任何信息在哪里放置授权 header(或我的问题的任何不同解决方案)。
打开 Web Socket 时弹出(基本登录形式,但经过改进):
打开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');
}
});
});
}
编辑:
在我将 header 添加到 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() 函数的第一个参数是 header object。
您可以在
中添加授权
https://stomp-js.github.io/stomp-websocket/codo/extra/docs-src/Usage.md.html
在打开 Web Socket 时,我被要求输入我的登录名和密码,但此时我已经登录了。如何让我的应用程序在自动打开 Web 套接字时传递凭据?我只使用基本身份验证,但我找不到任何信息在哪里放置授权 header(或我的问题的任何不同解决方案)。
打开 Web Socket 时弹出(基本登录形式,但经过改进):
打开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');
}
});
});
}
编辑:
在我将 header 添加到 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() 函数的第一个参数是 header object。 您可以在
中添加授权https://stomp-js.github.io/stomp-websocket/codo/extra/docs-src/Usage.md.html