Apollo GraphQL:如何设置安全的 Websockets?
Apollo GraphQL: How to Set Up Secure Websockets?
我正在设置我的开发系统以使用 https
,Chrome 抱怨我的 websocket 未开始安全:
VM4965:161 Mixed Content: The page at 'https://mywebsite.io/' was
loaded over HTTPS, but attempted to connect to the insecure WebSocket
endpoint 'ws://mywebsite.io:4000/subscriptions'. This request has
been blocked; this endpoint must be available over WSS.
这是我当前的 WS 服务器端设置,基于 Apollo 文档:
const localHostString = 'mywebsite.io';
const pubsub = new PubSub();
// additional context you use for your resolvers, if any
const context = {connectors: connectors};
//SET UP APOLLO QUERY / MUTATIONS / PUBSUB
//start a graphql server with Express handling a possible Meteor current user
createApolloServer({
schema,
context
});
const METEOR_PORT = 3000;
const GRAPHQL_PORT = 4000;
const server = express();
server.use('*', cors({ origin: `https://${localHostString}:${METEOR_PORT}` }));
server.use('/graphql', bodyParser.json(), graphqlExpress({
schema,
context
}));
server.use('/graphiql', graphiqlExpress({
endpointURL: '/graphql',
subscriptionsEndpoint: `ws://${localHostString}:${GRAPHQL_PORT}/subscriptions`
}));
// Wrap the Express server
const ws = createServer(server);
ws.listen(GRAPHQL_PORT, () => {
console.log(`GraphQL Server is now running on http://${localHostString}:${GRAPHQL_PORT}`);
console.log(`GraphiQL available at http://${localHostString}:${GRAPHQL_PORT}/graphiql`);
// Set up the WebSocket for handling GraphQL subscriptions
new SubscriptionServer({
execute,
subscribe,
schema
}, {
server: ws,
path: '/subscriptions',
});
});
我如何更新它以便使用 WSS 而不是 WS websockets?
提前感谢大家提供的任何信息。
看起来你在做
subscriptionsEndpoint: `ws://${localHostString}:${GRAPHQL_PORT}/subscriptions
或许可以将 ws
改为 wss
。即:
subscriptionsEndpoint: `wss://${localHostString}:${GRAPHQL_PORT}/subscriptions
我正在设置我的开发系统以使用 https
,Chrome 抱怨我的 websocket 未开始安全:
VM4965:161 Mixed Content: The page at 'https://mywebsite.io/' was loaded over HTTPS, but attempted to connect to the insecure WebSocket endpoint 'ws://mywebsite.io:4000/subscriptions'. This request has been blocked; this endpoint must be available over WSS.
这是我当前的 WS 服务器端设置,基于 Apollo 文档:
const localHostString = 'mywebsite.io';
const pubsub = new PubSub();
// additional context you use for your resolvers, if any
const context = {connectors: connectors};
//SET UP APOLLO QUERY / MUTATIONS / PUBSUB
//start a graphql server with Express handling a possible Meteor current user
createApolloServer({
schema,
context
});
const METEOR_PORT = 3000;
const GRAPHQL_PORT = 4000;
const server = express();
server.use('*', cors({ origin: `https://${localHostString}:${METEOR_PORT}` }));
server.use('/graphql', bodyParser.json(), graphqlExpress({
schema,
context
}));
server.use('/graphiql', graphiqlExpress({
endpointURL: '/graphql',
subscriptionsEndpoint: `ws://${localHostString}:${GRAPHQL_PORT}/subscriptions`
}));
// Wrap the Express server
const ws = createServer(server);
ws.listen(GRAPHQL_PORT, () => {
console.log(`GraphQL Server is now running on http://${localHostString}:${GRAPHQL_PORT}`);
console.log(`GraphiQL available at http://${localHostString}:${GRAPHQL_PORT}/graphiql`);
// Set up the WebSocket for handling GraphQL subscriptions
new SubscriptionServer({
execute,
subscribe,
schema
}, {
server: ws,
path: '/subscriptions',
});
});
我如何更新它以便使用 WSS 而不是 WS websockets?
提前感谢大家提供的任何信息。
看起来你在做
subscriptionsEndpoint: `ws://${localHostString}:${GRAPHQL_PORT}/subscriptions
或许可以将 ws
改为 wss
。即:
subscriptionsEndpoint: `wss://${localHostString}:${GRAPHQL_PORT}/subscriptions