---
sidebar_position: 7
slug: /dev/messenger
---

# 98.7、信标处理


### 1、添加一个信标处理
:::tip[说明]

添加自定义信标处理

```
//初始化
LinkerMessengerEntry.Initialize();
//在这里注入
LinkerMessengerEntry.AddService<MyMessenger>();

//构建
LinkerMessengerEntry.Build();

//添加进去
IMessengerResolver messengerResolver = serviceProvider.GetService<IMessengerResolver>();
messengerResolver.AddMessenger(new List<IMessenger> { serviceProvider.GetService<MyMessenger>() });

//运行
LinkerMessengerEntry.Setup();

//使用 MessengerId 标注方法，设置一个id，客户端发送数据时指定id，
// 返回值可以是 void, T, Task, Task<T>
// 返回值是 void Task 时也可以手动 IMessengerSender.ReplyOnly 进行回复
public sealed class MyMessenger: IMessenger
{
    private readonly IMessengerSender sender;
    public MyMessenger(IMessengerSender sender)
    {
        this.sender = sender;
    }
    [MessengerId(3200)]
    public void Method0(IConnection connection)
    {
        //发送来的数据
        ReadOnlyMemory<byte> request = connection.ReceiveRequestWrap.Payload;

        uint requiestid = connection.ReceiveRequestWrap.RequestId;
        await sender.ReplyOnly(new MessageResponseWrap
        {
            RequestId = requiestid,
            Connection = connection,
            Payload = 回复的数据
        }, 3200);//表示从3200回复的
        .ConfigureAwait(false);
    }
    [MessengerId(3201)]
    public async Task Method1(IConnection connection)
    {
    }
    [MessengerId(3202)]
    public async Task<bool> Method2(IConnection connection)
    {
    }
}
```
:::

### 2、给自定义的信标发送数据
:::tip[说明]

发送数据/发送数据并获取返回值

```
//初始化
LinkerMessengerEntry.Initialize();
//在这里注入
LinkerMessengerEntry.AddService<MyMessengerHelper>();

//构建
LinkerMessengerEntry.Build();
//运行
LinkerMessengerEntry.Setup();

//依赖注入获取类
MyMessengerHelper myMessengerHelper = LinkerMessengerEntry.GetService<MyMessengerHelper>();
//myMessengerHelper.SendData();

//假设这是你的操作类
public sealed class MyMessengerHelper
{
    //信标发送
    private readonly IMessengerSender sender;
    //登录状态
    private readonly SignInClientState signInClientState;
    //序列化
    private readonly ISerializer serializer;
    public MyMessengerHelper(IMessengerSender sender,SignInClientState signInClientState,
    ISerializer serializer)
    {
        this.sender = sender;
        this.signInClientState = signInClientState;
        this.serializer = serializer;
    }
    public async Task<bool> SendData(Memory<byte> data)
    {
        return await sender.SendOnly(new MessageRequestWrap
        {
            Connection = signInClientState.Connection,
            MessengerId = 3200,
            Payload = data
        });
    }
    public T SendDataAndGetReply<T>(Memory<byte> data)
    {
        MessageResponeInfo resp = await sender.SendReply(new MessageRequestWrap
        {
            Connection = signInClientState.Connection,
            MessengerId = 3200,
            Payload = data
        });
        if (resp.Code == MessageResponeCodes.OK && resp.Data.Length > 0)
        {
            return serializer.Deserialize<T>(resp.Data.Span);
        }
        return null;
    }
}
```
:::