与“ws://localhost:4000/graphql”的 WebSocket 连接失败:
P粉344355715
P粉344355715 2023-08-29 23:58:21
[Vue.js讨论组]
<p>我现在在客户端和服务器端都收到此 <em>Websocket 无法连接</em> 错误(如下图所示)。除了 apollo 客户端中指定的配置之外,我没有使用任何其他 Websocket 配置。这让我困惑了大约两天。任何帮助,将不胜感激。如果您需要查看任何进一步的代码,请告诉我。</p> <p>我有一个连接到 <strong>graphql apollo 服务器</strong>的 <strong>Vue 应用客户端</strong>。下面给出apolloclient配置的代码。</p> <p> <pre class="snippet-code-js lang-js prettyprint-override"><code>// Apollo packages import { ApolloClient } from "apollo-boost-upload"; import { WebSocketLink } from "apollo-link-ws"; import { HttpLink } from "apollo-link-http"; import { InMemoryCache } from "apollo-cache-inmemory"; import { split } from "apollo-link"; import { getMainDefinition } from "apollo-utilities"; import VueApollo from "vue-apollo"; Vue.use(VueApollo); wsLink = new WebSocketLink({ uri: "ws://localhost:4000/graphql", // use wss for a secure endpoint options: { reconnect: true, }, }); const link = split( // split based on operation type ({ query }) =&gt; { const definition = getMainDefinition(query); return ( definition.kind === "OperationDefinition" &amp;&amp; definition.operation === "subscription" ); }, wsLink, httpLink ); // Cache implementation export const defaultClient = new ApolloClient({ // uri: "http://localhost:4000/graphql", link, cache: new InMemoryCache(), fetchOptions: { credentials: "include", }, request: (operation) =&gt; { // if no token in local storage, add it if (!localStorage.anaceptToken) { localStorage.setItem("anaceptToken", ""); } // operation adds the token to authorizatrion header, which is sent o backend operation.setContext({ headers: { authorization: "Bearer " + localStorage.getItem("anaceptToken"), }, }); }, onError: ({ graphQLErrors, networkError }) =&gt; { if (networkError) { console.log("[networkError]", networkError); } if (graphQLErrors) { for (const error of graphQLErrors) { console.dir(error); console.log(error); if ( error.name === "AuthenticationError" || error.message === "jwt expired" ) { // set auth error in state store.commit("setError", error); // signout user to clear error store.dispatch("signUserOut"); } } } }, });</code></pre> </p> <p>vue 配置文件</p> <pre class="brush:php;toolbar:false;">const { defineConfig } = require(&quot;@vue/cli-service&quot;); const NodePolyfillPlugin = require(&quot;node-polyfill-webpack-plugin&quot;); module.exports = defineConfig({ pluginOptions: { apollo: { enableMocks: true, enableEngine: true, }, }, transpileDependencies: [&quot;vuetify&quot;], chainWebpack: (config) =&gt; { config.performance.maxEntrypointSize(400000).maxAssetSize(400000); new NodePolyfillPlugin(); }, });</pre>
P粉344355715
P粉344355715

全部回复(1)
P粉914731066

有趣的尝试 localhost 4004,它应该可以工作

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号