Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular NgStomp在InjectableRxStompConfig中使用beforeConnect_Angular_Websocket_Promise_Stomp_Stompjs - Fatal编程技术网

Angular NgStomp在InjectableRxStompConfig中使用beforeConnect

Angular NgStomp在InjectableRxStompConfig中使用beforeConnect,angular,websocket,promise,stomp,stompjs,Angular,Websocket,Promise,Stomp,Stompjs,我正在尝试构建一个使用WebSocket的angular客户端,我正在使用@stomp/ng2 stompjs 我遵循这个指南 除了我需要在ConnectHeader中设置用于验证服务器端的令牌之外,其他一切都可以正常工作。为此,我了解到我需要在连接之前使用该函数 我尝试了各种方式,但我不知道如何使用它,它会卡住或发送一个空令牌 我的rxStompConfig: 导出类RxStompConfig扩展了InjectableRxStompConfig{ constructor(private use

我正在尝试构建一个使用WebSocket的angular客户端,我正在使用@stomp/ng2 stompjs 我遵循这个指南

除了我需要在ConnectHeader中设置用于验证服务器端的令牌之外,其他一切都可以正常工作。为此,我了解到我需要在连接之前使用该函数

我尝试了各种方式,但我不知道如何使用它,它会卡住或发送一个空令牌

我的rxStompConfig:

导出类RxStompConfig扩展了InjectableRxStompConfig{

constructor(private userService: UserService) {
    super();
    this.brokerURL = env.wsServerBaseUrl;

    // Interval in milliseconds, set to 0 to disable
    this.heartbeatIncoming = 0; // Typical value 0 - disabled
    this.heartbeatOutgoing = 20000; // Typical value 20000 - every 20 seconds

    // Wait in milliseconds before attempting auto reconnect
    // Set to 0 to disable
    // Typical value 500 (500 milli seconds)
    this.reconnectDelay = 5000;
    // Will log diagnostics on console
    // It can be quite verbose, not recommended in production
    // Skip this key to stop logging to console
    this.debug = (msg: string): void => {
        console.log(new Date(), msg);
    };
    console.log('Constructor ' + this.connectHeaders);

    this.beforeConnect = (): Promise<void> => {
        return new Promise<void>((resolve, reject) => {
            this.userService.currentToken.subscribe(token => {
                console.log('Subscribed');
                if (token) {
                    console.log('Resolved ' + token);
                    this.connectHeaders = { Authorization: `Bearer ${token}`};
                    resolve();
                }
            });
        });
    };
}
构造函数(私有用户服务:用户服务){
超级();
this.brokerURL=env.wsServerBaseUrl;
//间隔(毫秒),设置为0禁用
this.heartbeatIncoming=0;//典型值0-禁用
this.heartbeatouting=20000;//典型值20000-每20秒
//在尝试自动重新连接之前,请以毫秒为单位等待
//设置为0以禁用
//典型值500(500毫秒)
这是指重新连接延迟=5000;
//将在控制台上记录诊断
//它可能非常冗长,不建议在生产中使用
//跳过此键可停止登录到控制台
this.debug=(msg:string):void=>{
log(新日期(),msg);
};
console.log('Constructor'+this.connectHeaders);
this.beforeConnect=():Promise=>{
返回新承诺((解决、拒绝)=>{
this.userService.currentToken.subscribe(令牌=>{
console.log('Subscribed');
如果(令牌){
console.log('Resolved'+令牌);
this.connectHeaders={Authorization:`Bearer${token}}};
解决();
}
});
});
};
}
我从进行身份验证的服务获取令牌并设置令牌

这是currentToken方法:

get currentToken(): Observable<string> {
    return this.$token.asObservable();
}
get currentToken():可观察{
返回此。$token.asObservable();
}
有人能帮我吗?

检查一下这个

有一个修复程序可以解决您的问题。
基本上,现在在beforeConnect中,您可以接收可以配置的stompClient,并且在您的特定情况下,您可以设置ConnectHeader

您确定
始终指向您期望的内容吗?