---
sidebar_position: 8
slug: /dev/resolver
---

# 98.8、分发器

:::tip[说明]

1. 服务端只监听了一个端口，所以需要分发器，才能使用一个端口实现不同的分离的业务逻辑
2. 类似于，信标，外网端口，中继，中继节点报告，都是通过分发器实现

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

//构建
LinkerMessengerEntry.Build();

//添加进去
ResolverTransfer resolverTransfer = serviceProvider.GetService<ResolverTransfer>();
resolverTransfer.AddResolvers(new List<IResolver> { serviceProvider.GetService<MyResolver>() });
//运行
LinkerMessengerEntry.Setup();

public class MyResolver : IResolver
{
    //建议从100开始用
    public byte Type => 101;

    public async Task Resolve(Socket socket, Memory<byte> memory)
    {
        //tcp,memory是可能存在的首包数据
    }
    public async Task Resolve(Socket socket, IPEndPoint ep, Memory<byte> memory)
    {
        //udp,memory是可能存在的首包数据
    }
}
```

```
//服务器
IPEndPoint ep = null;

Socket socket = new Socket(ep.AddressFamily, SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
socket.KeepAlive();
await socket.ConnectAsync(ep).WaitAsync(TimeSpan.FromMilliseconds(5000)).ConfigureAwait(false);
//连接成功后首字节发送你的信标标识
await socket.SendAsync(new byte[] { 101 });

//接下来就可以做你喜欢的事儿了
```
:::