gpt4 book ai didi

useSubscription Socket errors being thrown in the console(在控制台中引发useSubscription套接字错误)

转载 作者:bug小助手 更新时间:2023-10-22 15:47:00 26 4
gpt4 key购买 nike



I hope you are doing well!

我希望你做得好!


We are using web sockets in our website with GraphQL, hence we are using useSubscription.

我们在GraphQL的网站中使用web套接字,因此我们使用useSubscription。


It is working fine, I get the data back when I need it; however, we are facing a small issue that while it is not stopping our website from functioning, it is extremely weird and awkward for users using the website.

它工作得很好,我需要的时候会把数据拿回来;然而,我们面临的一个小问题是,虽然这并没有阻止我们的网站运行,但对于使用网站的用户来说,这是非常奇怪和尴尬的。


Basically, randomly, without any call from the backend web socket the useSubscription sometimes throws multiple errors in the console at no specific time, very randomly.

基本上,在没有来自后端web套接字的任何调用的情况下,useSubscription有时会在没有特定时间、非常随机地在控制台中抛出多个错误。


There are 2 errors:

有2个错误:


The first one is:

第一个是:



WebSocket connection to URL failed: Insufficient resources.



The second one is:

第二个是:



WebSocket connection to URL failed: WebSocket is closed before the connection is established.



With URL being replaced with our backend WebSocket URL.

URL被替换为我们的后端WebSocket URL。


We know that the URL is valid since it works perfectly fine, and we get data when needed. However, this error is weird since we get it at random times without any call from the backend web socket.

我们知道URL是有效的,因为它运行得非常好,并且我们在需要时获取数据。然而,这个错误很奇怪,因为我们在没有任何来自后端web套接字的调用的情况下随机获得它。


We are using GraphQL and @apollo/client to handle it. And we are getting our SubscriptionClient from subscriptions-transport-ws.

我们正在使用GraphQL和@apolo/client来处理它。我们正在从订阅传输ws获取我们的SubscriptionClient。


Here is our code for the initialization of the client :

以下是我们用于初始化客户端的代码:


import { ApolloClient, InMemoryCache, split } from "@apollo/client";
import { getMainDefinition } from "@apollo/client/utilities";
import { HttpLink } from "apollo-link-http";
import { WebSocketLink } from '@apollo/client/link/ws';
import { SubscriptionClient } from 'subscriptions-transport-ws';
import { link, socketLink } from "./gql-link";

const httpLink = new HttpLink({
uri: link,
});

const subscriptionClient = new SubscriptionClient(socketLink, {
reconnect: true,
});

const wsLink = new WebSocketLink(subscriptionClient);

const splitLink = split(
({ query }) => {
const { kind, operation } = getMainDefinition(query);

return kind === "OperationDefinition" && operation === "subscription";
},
wsLink,
httpLink
);

const client = new ApolloClient({
link: splitLink,
cache: new InMemoryCache({addTypename: false}),

});

export default client;



Thank You a lot for your help in advanced!

非常感谢您的帮助!


更多回答
优秀答案推荐
更多回答

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com