简体   繁体   English

如何修复前端的 RXStomp 客户端有时在生产服务器上不接收数据?

[英]How do I fix my RXStomp client on the frontend not receiving data sometimes on production server?

Here is my configuration for RXStomp on frontend;这是我在前端对 RXStomp 的配置;

import { RxStompConfig } from '@stomp/rx-stomp';
import { environment } from '../../../environments/environment';

export const myRxStompConfig: RxStompConfig = {

  reconnectDelay: 20000,

  debug: (msg: string): void => {
    if (!environment.production) {
      console.log(msg);
    }
  },
};

Here is the code for the rxStompService;这是 rxStompService 的代码;

import { Injectable } from "@angular/core";
import { RxStomp } from "@stomp/rx-stomp";
import SockJS from "sockjs-client";
import { environment } from "../../../environments/environment";
import { myRxStompConfig } from "../configurations/rx-stomp.config";

@Injectable({
  providedIn: "root",
})
export class RxStompService extends RxStomp {
  public currentRetry  = 0;

  public resetRetry() {
    this.currentRetry = 0;
  }
}

export function rxStompServiceFactory() {
  const rxStomp = new RxStompService();
  myRxStompConfig.webSocketFactory = function () {
    return new SockJS(`${environment.baseUrl}/public/chatsocket`);

  };
  rxStomp.resetRetry();
  myRxStompConfig.beforeConnect = (): Promise<void> => {
    return new Promise<void>((resolve, reject) => {
      if (rxStomp.currentRetry <= 5) {
        rxStomp.currentRetry++;
        resolve();
      }
    });
  };
  rxStomp.configure(myRxStompConfig);
  rxStomp.activate();
  return rxStomp;
}

The websocket was working fine until a couple of days ago when the socket broke on the production server and then we changed the URL of the websocket on the backend from public/websocket to public/chatsocket . websocket 工作正常,直到几天前生产服务器上的套接字坏了,然后我们将后端 websocket 的 URL 从public/websocket更改为public/chatsocket The socket gets connected and then I subscribe to the required channel using套接字已连接,然后我使用订阅所需的频道

this.rxStompService.watch(`/user/${id}/message`).subscribe((message) => {
   console.log(message.body);
};

The messages send correctly on the backend (Springboot) using this code;使用此代码在后端(Springboot)正确发送消息;

private void sendWebMessageToConversation(MessageResponseDto messageResponseDto, String destinationId) {
        try{
            simpMessagingTemplate.convertAndSendToUser(destinationId, "/message", messageResponseDto);
        }
        catch (Exception e){
            logger.error(e.getMessage(), e);
        }
    }

After debugging we found that the message gets filtered correctly and sent to all the users in a particular conversation via simpMessagingTemplate.convertAndSendToUser but the message does not get received on the frontend socket client RXStomp, even though the socket connection is established and the client is subscribed/watching the correct channel.调试后,我们发现消息被正确过滤并通过simpMessagingTemplate.convertAndSendToUser发送给特定对话中的所有用户,但是前端套接字客户端 RXStomp 没有收到消息,即使套接字连接已建立并且客户端已订阅/正在观看正确的频道。

This feature works correctly on all of the test environments but for some reason there is an inconsistency when working on the production server.此功能在所有测试环境中都能正常工作,但由于某种原因在生产服务器上工作时会出现不一致。 Sometimes one user subscribed to the channel will receive messages but the other won't.有时,一个订阅频道的用户会收到消息,而另一个则不会。 Or both users won't receive messages, or the feature works correctly and both receive messages properly.或者两个用户都不会收到消息,或者该功能正常工作并且两个用户都可以正常接收消息。 How do I fix this to make it work correctly all the time?如何解决此问题以使其始终正常工作?

It is difficult to pinpoint issue without seeing more details.在没有看到更多细节的情况下很难查明问题。 I believe the issue could most probably is because of the fact that there is no acknowledgement from frontend.我相信这个问题很可能是因为没有来自前端的确认。 Your code posted doesn't seem to have acknowledgement.您发布的代码似乎没有确认。

By default stomp is using auto ack.默认情况下,stomp 使用auto确认。 This will just send the message to frontend but won't ensure client received data.这只会将消息发送到前端,但不能确保客户端收到数据。 You probably will have to change the ack to client or client-individual depending on your requirement.您可能必须根据您的要求将确认更改为clientclient-individual

Check this link to see if you need to implement this for acknowledgement.检查链接以查看您是否需要实施此确认。

I also couldn't see any reconnection logic implemented.我也看不到任何重新连接逻辑的实现。 Try to implement that as well.也尝试实现它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在前端显示来自 Firestore 的数据 - How do I to display data from Firestore on frontend 在客户端使用 Ionic 5 和 angular,在服务器端使用 Flask,如何加密我的 http URL 参数? - Using Ionic 5 and angular on client side and Flask on server side, how do I encrypt my http URL parameters? 如何修复 ng build for production 上的“The shadow-piercing descendant combinator (>>>) is deprecated”警告 - How do I fix the "The shadow-piercing descendant combinator (>>>) is deprecated" warning on ng build for production 如何通过部署在 Heroku 上的 Nodejs/Nestjs 服务器为我的 Angular 前端提供服务? - How can I serve my Angular frontend through a Nodejs/Nestjs server deployed on Heroku? 订阅 http get 调用 - 为什么我需要刷新我的前端页面才能在数据被更改后获取数据? - subscribing a http get call - why do I need to refresh my frontend page to get the data after it got altered? 当我在前端(离子)中调用一个方法(使用邮递员到后端对其进行测试时可以正常工作)时,收到内部服务器错误500 - Receiving Internal Server Error 500 when I call a method in the frontend (Ionic) that works correctly when I test it using postman to the backend 如何使用 `npm run build: production` 而不是使用`npm run build --production` 构建我的 angular 应用程序? - How do i build my angular application with `npm run build : production` instead of using `npm run build --production`? 为什么有时我需要刷新网页才能看到我从 api 检索到的数据出现在我的表上? 角 9 - Why do I need to refresh the web page sometimes to see the data i retrieved from the api appear on my table? Angular 9 Angular 13,从 API 接收数据,现在我该如何使用它? - Angular 13, receiving data from API, now how do I use it? 如何为角度2创建生产构建? - How do i create production build for angular 2?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM