与“ws://localhost:4000/graphql”的 WebSocket 连接失败:
P粉344355715
P粉344355715 2023-08-29 23:58:21
[Vue.js讨论组]

我现在在客户端和服务器端都收到此 Websocket 无法连接 错误(如下图所示)。除了 apollo 客户端中指定的配置之外,我没有使用任何其他 Websocket 配置。这让我困惑了大约两天。任何帮助,将不胜感激。如果您需要查看任何进一步的代码,请告诉我。

我有一个连接到 graphql apollo 服务器Vue 应用客户端。下面给出apolloclient配置的代码。

// 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 }) => {
        const definition = getMainDefinition(query);
        return (
          definition.kind === "OperationDefinition" &&
          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) => {
        // 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 }) => {
        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");
            }
          }
        }
      },
    });

vue 配置文件

const { defineConfig } = require("@vue/cli-service");
const NodePolyfillPlugin = require("node-polyfill-webpack-plugin");
module.exports = defineConfig({
  pluginOptions: {
    apollo: {
      enableMocks: true,
      enableEngine: true,
    },
  },
  transpileDependencies: ["vuetify"],
  chainWebpack: (config) => {
    config.performance.maxEntrypointSize(400000).maxAssetSize(400000);
    new NodePolyfillPlugin();
  },
});
P粉344355715
P粉344355715

全部回复(1)
P粉914731066

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

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

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