com.cneicy.shrink-network-integration-eventbus (0.1.2)
Published 2026-08-26 12:51:49 +08:00 by cneicy
Installation
registry=https://git.crash.work/api/packages/ShrinkSDK/npm/npm install com.cneicy.shrink-network-integration-eventbus@0.1.2"com.cneicy.shrink-network-integration-eventbus": "0.1.2"About this package
ShrinkNetwork 与 ShrinkEventBus 2.0 的生成式桥接层,让 IShrinkEvent 网络事件可广播、裁决并在远端重新进入命名 Bus。
ShrinkNetwork.Integration.EventBus
把实现 IShrinkEvent 的网络事件与 ShrinkEventBus 2.0 连接。广播事件可从本地 Bus 转发到远端;请求事件可在远端 PostAsync 后回传 EventResult 与取消状态;delta 事件按版本去重。
广播事件
[ShrinkNetworkEvent]
[ShrinkNetworkMessage(3001, "room/player_ready")]
public sealed class PlayerReadyEvent : IShrinkEvent, IShrinkNetworkMessage
{
public string PlayerId { get; set; } = string.Empty;
}
EventBus.Post(new PlayerReadyEvent { PlayerId = "10001" });
await session.PublishEventAsync(new PlayerReadyEvent { PlayerId = "10001" });
事件必须同时实现 IShrinkEvent、IShrinkNetworkMessage,并声明 [ShrinkNetworkEvent] 与 [ShrinkNetworkMessage]。本包的 ILPostProcessor 为每个事件生成强类型入站 dispatcher 和模块注册,不在运行时扫描程序集、构造泛型方法或反射调用 handler。
远端裁决
[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;
}
var outcome = await session.RequestEventAsync(new CanUseSkillEvent());
请求事件必须实现 IShrinkResultEvent<EventResult> 与 IShrinkNetworkRequest。如果需要取消回传,再实现 IShrinkCancelableEvent。当前响应只包含 EventResult、取消状态和 RPC 错误,不自动回传事件上的其它可变字段。
Delta
实现 IShrinkNetworkDeltaEvent 后,入站按 SessionId + EventType + DeltaKey 记录最高 DeltaVersion;旧版本和重复版本不会进入 Bus。
接入
ContextLoader 默认通过 ShrinkNetworkEventBusComponent 接桥。Standalone:
service.UseEventBusBridge(new ShrinkNetworkEventBusBridgeOptions
{
SessionFilter = (session, value) => session.SessionId > 0
});
可用发送扩展:
session.PublishEventAsync(eventData);
session.RequestEventAsync(eventData);
service.BroadcastEventAsync(eventData);
远端事件重入 Bus 时会抑制回环转发。网络桥只观察 EventBus 全局宿主管理的 Bus;独立 ShrinkEventBusHost 不会被自动网络转发。
Dependencies
Dependencies
| ID | Version |
|---|---|
| com.cneicy.shrink-context-core | 0.1.0 |
| com.cneicy.shrink-eventbus | 2.0.0 |
| com.cneicy.shrink-network | 0.2.1 |
Keywords
network
eventbus
integration
bridge
sync