> 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/get-friends.md).

# 获取好友

关于在 Decentraland 平台中使用 Social Service API 来检索好友关系、请求和共同好友的详细信息。该 API 允许开发者与 Social Service 交互以检索与好友关系相关的信息。

#### REST API

要检索好友关系，Social Service 提供了 [REST API](https://social-service-api-specs.pages.dev/):

* `GET /friendships/me` - 检索当前用户的好友关系。需要在 Authorization 头中提供访问令牌。
* `GET /friendships/{other-address}/mutuals` - 检索与另一位用户的共同好友。需要在 Authorization 头中提供访问令牌。

#### WebSocket RPC 服务器

Social Service WebSocket API 提供两种用于检索好友关系和共同好友的消息类型。你需要先建立 WebSocket 连接，然后使用这些方法来流式获取与用户相关的信息。

* `GetFriends` - 检索某个用户的好友。返回用户响应流。
* `GetMutualFriends` - 检索与另一位用户的共同好友。返回用户响应流。

请参阅 [friendships.proto 文件](https://github.com/decentraland/protocol/blob/main/proto/decentraland/social/friendships/friendships.proto#L7) 以了解有关这些消息类型的更多详情。

### JavaScript 代码示例

要使用 JavaScript 与 Social Service WebSocket API 交互，你可以像前面说明的那样使用提供的 RPC 客户端。但如果你需要自行实现，下面有一些代码示例，演示如何检索好友和共同好友：

#### 获取所有好友

```javascript
import { createRpcClient, createWebSocketsTransport } from '@dcl/rpc/dist/client';
import { FriendshipsServiceDefinition } from './protobuff-types/decentraland/social/friendships/friendships.gen';

const socialClientRpcUrl = 'wss://social.decentraland.org'; // 替换为实际 URL
const webSocketsTransport = createWebSocketsTransport(socialClientRpcUrl);
const service = loadService(FriendshipsServiceDefinition, webSocketsTransport);

const response = service.getFriends(Payload.create({ synapseToken }));
for await (const friends of response) {
  processErrors(friends);
  const userList = friends.users?.users ?? [];
  // 处理好友列表
}
```

#### 获取共同好友

```javascript
const response = service.getMutualFriends(
  MutualFriendsPayload.create({
    user: { address }, 
    authToken: { synapseToken }
  })
);
for await (const mutualFriend of response) {
  processErrors(mutualFriend);
  const mutualFriendList = mutualFriend.users?.users ?? [];
  // 处理共同好友列表
}
```

更多代码示例和详细信息，请访问 [social-rpc-client-js GitHub 仓库](https://github.com/decentraland/social-rpc-client-js)


---

# 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/get-friends.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.
