简体   繁体   English

如何在阿波罗订阅服务器上获取客户端 IP 地址?

[英]How to get client ip address on an apollo subscriptions server?

How can I get the client's ip address on an apollo subscriptions server?如何在 apollo 订阅服务器上获取客户端的 IP 地址?

Is it included anywhere in the .onConnect method here它是否包含在此处.onConnect方法中的任何位置

You can get client's ip address by using context.request.connection.remoteAddress .您可以使用context.request.connection.remoteAddress获取客户端的 IP 地址。

Eg例如

import http from 'http';
import { ApolloServer, gql, PubSub } from 'apollo-server-express';
import express from 'express';

const pubsub = new PubSub();
const POST_ADDED = 'POST_ADDED';
const db: { posts: any[] } = {
  posts: [],
};

const typeDefs = gql`
  type Subscription {
    postAdded: Post
  }

  type Query {
    posts: [Post]
  }

  type Mutation {
    addPost(author: String, comment: String): Post
  }

  type Post {
    author: String
    comment: String
  }
`;
const resolvers = {
  Subscription: {
    postAdded: {
      subscribe: () => pubsub.asyncIterator([POST_ADDED]),
    },
  },
  Query: {
    posts(root, args, context) {
      return db.posts;
    },
  },
  Mutation: {
    addPost(root, args, context) {
      pubsub.publish(POST_ADDED, { postAdded: args });
      const post = { ...args };
      db.posts.push(post);
    },
  },
};

const PORT = 4000;
const app = express();
const server = new ApolloServer({
  typeDefs,
  resolvers,
  subscriptions: {
    onConnect: (connectionParams, webSocket, context) => {
      console.log('remote address: ', context.request.connection.remoteAddress);
      console.log('websocket connected');
    },
    onDisconnect: (webSocket, context) => {
      console.log('websocket disconnected');
    },
  },
});

server.applyMiddleware({ app });

const httpServer = http.createServer(app);
server.installSubscriptionHandlers(httpServer);

httpServer.listen(PORT, () => {
  console.log(`🚀 Server ready at http://localhost:${PORT}${server.graphqlPath}`);
  console.log(`🚀 Subscriptions ready at ws://localhost:${PORT}${server.subscriptionsPath}`);
});

Debug logs:调试日志:

🚀 Server ready at http://localhost:4000/graphql
🚀 Subscriptions ready at ws://localhost:4000/graphql
remote address:  ::1
websocket connected

I test it localhost, so the client IP address is ::1 .我测试它本地主机,所以客户端 IP 地址是::1 ::1 is a "loopback address in IPv6", aka localhost. ::1是“IPv6 中的环回地址”,又名 localhost。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SMPP 服务器 - 如何获取客户端(ESME)IP 地址? - SMPP server - How to get the client (ESME's) IP address? 使用单独的客户端和服务器应用程序,如何在带有Koa的Node中获取用户的IP地址? - With separate client and server apps, how do I get a user's IP address, in Node with Koa? 在服务器客户端上连接之前如何获取IP地址(使用websockets / ws)? - How get IP address before connecting on server client (use websockets/ws)? 如何通过 Apollo 客户端在 NodeJS 中进行 GraphQL 订阅? - How do I make GraphQL subscriptions in NodeJS via Apollo Client? 如何在 next.js 应用程序中从服务器端获取客户端的 ip 地址? - how to get the ip address of the client from server side in next.js app? 在带有 Nest.js 的 Node 中,当客户端和服务器应用程序分开时,如何获取用户的 IP 地址 - how to I get a user's IP address when separate client and server apps, in Node with Nest.js 订阅不适用于 Apollo Server Express - Subscriptions not working on Apollo Server Express Apollo Server Express + 订阅错误 - Apollo Server Express + Subscriptions error 如何使用socket.io获取客户端IP地址? - How can I get the client IP address with socket.io? 如何在socket.io中获取客户端IP地址 - how to get client ip address in socket.io
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM