我正在设置我的开发系统以使用 https
,Chrome 提示我的 websocket 不安全:
这是我当前的 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
。 IE:subscriptionsEndpoint: `wss://${localHostString}:${GRAPHQL_PORT}/subscriptions
关于websocket - Apollo GraphQL : How to Set Up Secure Websockets?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46108769/