gpt4 book ai didi

meteor - Apollo 2.x 客户端和服务器设置 : Error during WebSocket handshake?

转载 作者:行者123 更新时间:2023-12-02 09:54:05 24 4
gpt4 key购买 nike

我已经让 Apollo 2.x 用于查询和突变。现在我正在致力于实现订阅的客户端和服务器设置。我收到浏览器控制台错误:

VM3719:164 WebSocket connection to 'ws://localhost:3200/' failed: Error during WebSocket handshake: Unexpected response code: 400

研究了许多可用资源后,我感觉我的代码非常接近正确,但我尚未解决此错误。

这是我当前的设置代码。

客户端

import React from "react";
import { Meteor } from "meteor/meteor";
import { render } from "react-dom";
import { ApolloProvider } from "react-apollo";
import { ApolloLink, from } from "apollo-link";
import { ApolloClient } from "apollo-client";
import { HttpLink } from "apollo-link-http";
import { InMemoryCache } from "apollo-cache-inmemory";
import { onError } from 'apollo-link-error';
import { split } from 'apollo-link';
import { WebSocketLink } from 'apollo-link-ws';
import { getMainDefinition } from 'apollo-utilities';

import App from "../../ui/App";

// Create an http link:
const httpLink = new HttpLink({
uri: Meteor.absoluteUrl("graphql"),
credentials: 'same-origin'
})

// Create a WebSocket link:
const wsLink = new WebSocketLink({
uri: `ws://localhost:3200/`,
options: {
reconnect: true
}
});

// using the ability to split links, you can send data to each link
// depending on what kind of operation is being sent
const splitLink = split(
// split based on operation type
({ query }) => {
const { kind, operation } = getMainDefinition(query);
return kind === 'OperationDefinition' && operation === 'subscription';
},
wsLink,
httpLink,
);

const authLink = new ApolloLink((operation, forward) => {
const token = Accounts._storedLoginToken();
operation.setContext(() => ({
headers: {
"meteor-login-token": token
}
}));
return forward(operation);
});

const client = new ApolloClient({
link: ApolloLink.from([
onError(({ graphQLErrors, networkError }) => {
if (graphQLErrors)
graphQLErrors.map(({ message, locations, path }) =>
console.log(
`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`,
),
);
if (networkError) console.log(`[Network error]: ${networkError}`);
}),
authLink,
splitLink
]),
cache: new InMemoryCache()
});


const ApolloApp = () => (
<ApolloProvider client={client}>
<App />
</ApolloProvider>
);

Meteor.startup(() => {
render(<ApolloApp />, document.getElementById("app"));
});

服务器

import { createApolloServer } from "meteor/apollo";
import { makeExecutableSchema } from "graphql-tools";
import merge from "lodash/merge";

import GoalsSchema from "../../api/goals/Goal.graphql";
import GoalsResolvers from "../../api/goals/resolvers";
import ResolutionsSchema from "../../api/resolutions/Resolutions.graphql";
import ResolutionsResolvers from "../../api/resolutions/resolvers";
import UsersSchema from "../../api/users/User.graphql";
import UsersResolvers from "../../api/users/resolvers";
import { createServer } from 'http';
import { SubscriptionServer } from 'subscriptions-transport-ws';
import { execute, subscribe } from 'graphql';

const typeDefs = [GoalsSchema, ResolutionsSchema, UsersSchema];

const resolvers = merge(GoalsResolvers, ResolutionsResolvers, UsersResolvers);

const schema = makeExecutableSchema({
typeDefs,
resolvers
});

createApolloServer({ schema });

const WS_PORT = 3200;

// Create WebSocket listener server
const websocketServer = createServer((request, response) => {
response.writeHead(404);
response.end();
});

// Bind it to port and start listening
websocketServer.listen(WS_PORT, () => console.log(
`Websocket Server is now running on http://localhost:${WS_PORT}`
));

const subscriptionServer = SubscriptionServer.create(
{
schema,
execute,
subscribe,
},
{
server: websocketServer,
path: '/graphql',
},
);

如何纠正这个错误?

提前非常感谢所有人提供的任何信息。

最佳答案

首先想到的是:

您的服务器监听端口 3200 并使用路由 graphql

const subscriptionServer = SubscriptionServer.create(
{
schema,
execute,
subscribe,
},
{
server: websocketServer,
path: '/graphql', // used url route
},
);

因此,您的客户端 Websocket 链接必须为单个 graphql 端点使用正确的路由。更改为:

// Create a WebSocket link:
const wsLink = new WebSocketLink({
uri: `ws://localhost:3200/graphql`, // correct websocket url
options: {
reconnect: true
}
});

关于meteor - Apollo 2.x 客户端和服务器设置 : Error during WebSocket handshake?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49591169/

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