> 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 protobuf](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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
