This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
# ShrinkNetwork.Integration.EventBus
|
||||
|
||||
把实现 `IShrinkEvent` 的网络事件与 ShrinkEventBus 2.0 连接。广播事件可从本地 Bus 转发到远端;请求事件可在远端 `PostAsync` 后回传 `EventResult` 与取消状态;delta 事件按版本去重。
|
||||
|
||||
## 广播事件
|
||||
|
||||
```csharp
|
||||
[ShrinkNetworkEvent]
|
||||
[ShrinkNetworkMessage(3001, "room/player_ready")]
|
||||
public sealed class PlayerReadyEvent : IShrinkEvent, IShrinkNetworkMessage
|
||||
{
|
||||
public string PlayerId { get; set; } = string.Empty;
|
||||
}
|
||||
```
|
||||
|
||||
```csharp
|
||||
EventBus.Post(new PlayerReadyEvent { PlayerId = "10001" });
|
||||
await session.PublishEventAsync(new PlayerReadyEvent { PlayerId = "10001" });
|
||||
```
|
||||
|
||||
事件必须同时实现 `IShrinkEvent`、`IShrinkNetworkMessage`,并声明 `[ShrinkNetworkEvent]` 与 `[ShrinkNetworkMessage]`。本包的 ILPostProcessor 为每个事件生成强类型入站 dispatcher 和模块注册,不在运行时扫描程序集、构造泛型方法或反射调用 handler。
|
||||
|
||||
## 远端裁决
|
||||
|
||||
```csharp
|
||||
[ShrinkNetworkEvent]
|
||||
[ShrinkNetworkMessage(3010, "room/can_use_skill")]
|
||||
public sealed class CanUseSkillEvent :
|
||||
IShrinkResultEvent<EventResult>, IShrinkCancelableEvent, IShrinkNetworkRequest
|
||||
{
|
||||
private EventResult _result;
|
||||
private bool _canceled;
|
||||
|
||||
public EventResult Result => _result;
|
||||
public bool IsCanceled => _canceled;
|
||||
public void SetResult(EventResult value) => _result = value;
|
||||
public void SetCanceled(bool value) => _canceled = value;
|
||||
}
|
||||
```
|
||||
|
||||
```csharp
|
||||
var outcome = await session.RequestEventAsync(new CanUseSkillEvent());
|
||||
```
|
||||
|
||||
请求事件必须实现 `IShrinkResultEvent<EventResult>` 与 `IShrinkNetworkRequest`。如果需要取消回传,再实现 `IShrinkCancelableEvent`。当前响应只包含 `EventResult`、取消状态和 RPC 错误,不自动回传事件上的其它可变字段。
|
||||
|
||||
## Delta
|
||||
|
||||
实现 `IShrinkNetworkDeltaEvent` 后,入站按 `SessionId + EventType + DeltaKey` 记录最高 `DeltaVersion`;旧版本和重复版本不会进入 Bus。
|
||||
|
||||
## 接入
|
||||
|
||||
ContextLoader 默认通过 `ShrinkNetworkEventBusComponent` 接桥。Standalone:
|
||||
|
||||
```csharp
|
||||
service.UseEventBusBridge(new ShrinkNetworkEventBusBridgeOptions
|
||||
{
|
||||
SessionFilter = (session, value) => session.SessionId > 0
|
||||
});
|
||||
```
|
||||
|
||||
可用发送扩展:
|
||||
|
||||
```csharp
|
||||
session.PublishEventAsync(eventData);
|
||||
session.RequestEventAsync(eventData);
|
||||
service.BroadcastEventAsync(eventData);
|
||||
```
|
||||
|
||||
远端事件重入 Bus 时会抑制回环转发。网络桥只观察 `EventBus` 全局宿主管理的 Bus;独立 `ShrinkEventBusHost` 不会被自动网络转发。
|
||||
Reference in New Issue
Block a user