我有网络套接字客户端的实现。它监听服务器1的响应。一旦客户端收到来自服务器1的响应,它就通过http连接到另一个服务器2,以获取对象。我正在寻找一个同步调用到服务器2。谁能告诉我,这是如何实现的?
示例代码:这是客户端监听服务器1的地方。
this.chatSubscription = this.stompClient.subscribe("/chat/" + id, (message) => {
console.log(message.body);
this.store.dispatch(new NotificationActions.TryNotificationsList({
url: this.utilService.host + 'message'
}));
});效果代码:这是客户端通过http连接到服务器2以获取对象的地方。这就是我试图设置一个锁的地方,以便只有一个请求被设置到服务器2。
@Effect({
dispatch: false
})
actionTryNotificationsList = this.actions$
.ofType(NotificationActions.TRY_NOTIFICATIONS_LIST)
.switchMap((actions: NotificationActions.TryNotificationsList) => {
const data = actions.payload;
return this.requestsService.getRequest(data.url)
.pipe(catchError(this.requestsService.handleError < any > ('Unable to connect to server')))
.map((response: any) => {
if (response instanceof HttpResponse) {
if (response) {
this.displayNotificationService.notificationsList$.next(notificationsList);
}
}
}getRequest函数代码:
getRequest(url: string) {
const tkn = this.cookieService.get("token");
const httpOptions = {
observe: 'body',
responseType: 'json',
headers: new HttpHeaders({
'token': tkn,
'Content-Type': 'text/plain'
})
};
const req = new HttpRequest("GET", url, httpOptions);
return this.httpClient.request(req);
}如果您需要任何其他信息,请告诉我。
请告诉我如何实现锁。任何帮助都是非常感谢的。
发布于 2018-09-17 22:29:33
您可以在状态中再添加一个字段,比如serverCalled: boolean,您将在第一次使用此效果时将其更改为true (在从服务器2获取数据之后)。在调用之上,你可以随时检查你是否在这个“会话”期间从你的商店请求了这些数据。或者甚至在您的示例代码中,您可以使用选择器订阅存储的更改,因此即使在分派此操作之前,您也可以获取该变量的实际值,并在有效负载中传递多一个字段,例如shouldLoadData: true | false,因此实际上您只需检查它,并将或不对服务进行该调用。希望它能有所帮助,至少我现在是这么看的。
https://stackoverflow.com/questions/52369661
复制相似问题