[英]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.您可能必须根据您的要求将确认更改为
client
或client-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.