> For the complete documentation index, see [llms.txt](https://docs.decentraland.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.decentraland.org/contributor/contributor-zh/she-jiao-fu-wu/js-client.md).

# JavaScript 客户端

本文档摘自 [social-rpc-client-js](https://github.com/decentraland/social-rpc-client-js#using-the-client)

#### 基本设置与使用

要使用客户端，请在你的 NPM 项目中安装该包：

```bash
npm install -S @dcl/social-rpc-client
```

从已安装的包中导入客户端创建函数：

```typescript
import { createSocialClient } from "@dcl/social-client";
```

通过向客户端提供以下内容来创建一个新的客户端实例：

1. Social Service 的 REST API URL
2. Social Service 的 Websocket 端点 URL
3. 用户地址（与用于签署身份的地址相同）
4. 由用户的钱包签名的身份。

```typescript
import { createSocialClient } from "@dcl/social-client";
import { Wallet } from 'ethers'

// 为测试目的生成一个随机钱包，或在生产环境中使用用户的钱包。
const wallet = Wallet.createRandom()
const identity = await createIdentity(wallet, expiration)

const socialClient = await createSocialClient(
  "https://social.decentraland.org",
  "wss://social-service.decentraland.org",
  wallet.address,
  identity
);
```

该 `createSocialClient` 会执行连接到 Social Service 所需的操作，并返回已连接的客户端。

使用客户端与 Social Service 交互：

```typescript
import { createSocialClient } from "@dcl/social-client";

const socialClient = await createSocialClient(
  "https://social.decentraland.org",
  "wss://social-service.decentraland.org",
  wallet.address,
  identity
);

const friends = socialClient.getFriends()
for await (const friend of friends) {
  console.log(friend)
}
```

客户端公开了可通过以下方式使用的方法： [social protobuff](https://github.com/decentraland/protocol/blob/main/public/social.proto) 以及一个 disconnect 方法，用于将客户端从 Social Service 断开。

#### 生成身份

要通过 Social Service 对用户进行身份验证，你需要为他们生成一个身份。为此， `@dcl/crypto` 库提供了 `Authenticator.initializeAuthChain` 方法。使用它为你的用户生成身份：

```typescript
  import { Wallet } from 'ethers'
  import { Authenticator } from '@dcl/crypto'
  // 为测试目的生成一个随机钱包，或在生产环境中使用用户的钱包。
  const userWallet = Wallet.createRandom()

  // 为用户生成一个身份。
  const address = await userWallet.getAddress()
  const ephemeralWallet = Wallet.createRandom()
  const payload = {
    address: ephemeralWallet.address,
    privateKey: ephemeralWallet.privateKey,
    publicKey: ephemeralWallet.publicKey
  }
  const identity = await Authenticator.initializeAuthChain(address, payload, expiration, (message: string) => signer.signMessage(message))
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.decentraland.org/contributor/contributor-zh/she-jiao-fu-wu/js-client.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
