我已经用rxjs 5表示法编写了此服务代码:
library(rvest)
url <- "https://www.askebsa.dol.gov/mewaview/View/Index/6219"
nodes <- html_nodes(read_html(url), 'div.question-inline, div.question')
data <- list()
for (i in nodes) {
n = trimws(html_text(html_node(i, xpath='./text()')))
if (length(html_nodes(i, 'code')) == 0) {
text <- html_nodes(i, xpath = '../address/code/text()')
v <- paste(trimws(text), collapse = '\r\n')
} else {
v <- html_text(html_nodes(i, 'code'))
}
data[[n]] = v
}
print(data)
我正在尝试将其重构为rxjs 6有效代码,而我现在已经知道了:
constructor(private _authService: AuthService) {
this._ws$ = WebSocketSubject.create<any>({
url: WS_URL,
protocol: this._authService.token
});
this._ws$.retryWhen(errors => {
// switchMap to retrieve the source error
return errors.switchMap(sourceErr => {
console.log('Retry WS.', sourceErr);
return Observable.timer(1000).map(() => Observable.of(true));
}
);
}
).subscribe(
msg => {
if ('channel_name' in msg) {
console.log('Channel name', msg.channel_name);
this._channelName = msg.channel_name;
this._authService.channelName = msg.channel_name;
}
this.subject$.next(msg);
console.log(msg);
},
err => {
console.log(err);
this.subject$.error(err);
},
() => this.subject$.complete()
);
}
但是我遇到以下错误:
constructor(private _authService: AuthService) {
this._ws$ = WebSocketSubject.create({
url: WS_URL,
protocol: this._authService.token
});
retryWhen(() => {
// switchMap to retrieve the source error
return switchMap(() => {
return timer(1000).pipe(map(() => of(true)));
}
);
}
).subscribe(
msg => {
if ('channel_name' in msg) {
this._channelName = msg.channel_name;
this._authService.channelName = msg.channel_name;
}
this.subject$.next(msg);
},
err => {
this.subject$.error(err);
},
() => this.subject$.complete()
);
我不知道在这里使用retryWhen函数的最佳方法是什么,以及如何在其中使用WebSocketSubject。
答案 0 :(得分:2)
Rxjs 6中上述代码的实现看起来像下面的
this._ws$.pipe(
retryWhen(errors =>
errors.pipe(
tap(val => console.log('Retry WS.', val)),
delay(1000)
)
)).subscribe( msg => {
if ('channel_name' in msg) {
this._channelName = msg.channel_name;
this._authService.channelName = msg.channel_name;
}
this.subject$.next(msg);
},err => {
this.subject$.error(err);
},() => this.subject$.complete()
);
有关使用Rxjs 6时重试的更多信息,请参考此处
https://www.learnrxjs.io/operators/error_handling/retrywhen.html