react-native - Apollo 客户端订阅在一段时间后未获取
问题描述
我有这个奇怪的错误,我无法缝合解决。我有一个反应原生应用程序,我在我的 android 平板电脑上使用。
我希望能够让应用程序永远打开并保持与服务器的连接,这样我就可以获得应用程序的订阅。但即使应用程序/平板电脑打开,它也会在夜间以某种方式失去连接。
然后在早上,我可以在查询和突变中恢复连接,但订阅挂钩不再起作用。除非我重新加载应用程序。任何想法为什么会这样?
我知道后端在应该发送数据的时候。但是该应用程序并没有在 useSubscription 挂钩中恢复它。
这是我的client.js
文件 =>
const cache = new InMemoryCache({
typePolicies: {
Subscription: {
fields: {
orderUpdatedByRestaurantId: {
// Merges together incoming and existing cache ref
merge(existing = [], incoming: any) {
const newEntries = incoming.filter(
(n) =>
!existing.some(
(y) => JSON.stringify(y) === JSON.stringify(n),
) && n,
);
return [...existing, ...newEntries];
},
},
},
},
},
});
const httpLink = createHttpLink({
uri: GRAPHQL_ENDPOINT,
credentials: true,
});
const client = new SubscriptionClient(SUBSCRIPTION_ENDPOINT, {
reconnect: true,
});
const wsLink = new WebSocketLink(client);
client.onConnected(() => {
console.log('connected');
Sentry.captureMessage(
`Subscription client connected at ${dayjs().format()}`,
);
});
client.onDisconnected(() => {
console.log('disconnected');
Sentry.captureMessage(
`Subscription client disconnected at ${dayjs().format()}`,
);
setTimeout(() => {
client.connect();
}, 300000);
});
const mainLink = split(
({ query }) => {
const { kind, operation } = getMainDefinition(query);
return (
kind === 'OperationDefinition' && operation === 'subscription'
);
},
wsLink,
httpLink,
);
解决方案
推荐阅读
- asp.net - 如何在使用 docker 容器化的 asp.net Core 中的项目之间共享类定义
- asp.net - ASP.NET WebForms 中的行详细信息数据表
- python - 使用 python 的逻辑语句将房价分类到一个类别
- algorithm - 生成树和最小生成树的区别
- python - 在 AWS Beanstalk 上安装要求之前升级 pip
- android - 在 recyclerView 中突出显示过滤后的文本
- python - For 循环获取 AttributeError:“系列”对象没有属性“天”
- python - Pandas Pivot 使用多行
- php - 根据两个条件使用php从mongodb中删除
- r - 如何缩短 geom_smooth 线以使其适合绘图的边缘?