iohao / ioGame

无锁异步化、事件驱动架构设计的 java netty 网络编程框架; 轻量级,无需依赖任何第三方中间件或数据库就能支持集群、分布式; 适用于网络游戏服务器、物联网、内部系统及各种需要长连接的场景; 通过 ioGame 你可以很容易的搭建出一个集群无中心节点、集群自动化、分布式的网络服务器;FXGL、Unity、UE、Cocos Creator、Godot、Netty、Protobuf、webSocket、tcp、socket;java Netty 游戏服务器框架;

Home Page:http://game.iohao.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

FlowContext 增加更新、获取元信息的便捷使用

iohao opened this issue · comments

FlowContext 增加更新、获取元信息的便捷使用

使用示例

class MyAttachment implements Attachment {
    @Getter
    long userId;
    String nickname;
}
void attachment(FlowContext flowContext) {
    /*
     * 不想扩展 FlowContext 子类时,推荐使用
     */

    // 获取元信息
    MyAttachment attachment = flowContext.getAttachment(MyAttachment.class);
    attachment.nickname = "渔民小镇";

    // [同步]更新 - 将元信息同步到玩家所在的游戏对外服中
    flowContext.updateAttachment(attachment);

    // [异步无阻塞]更新 - 将元信息同步到玩家所在的游戏对外服中
    flowContext.updateAttachmentAsync(attachment);
}

扩展 FlowContext 方式的使用示例

通过扩展 FlowContext ,内部已经确定了自定义元信息;在重写 getAttachment 方法后,能更便捷的使用元信息。

void attachment(MyFlowContext flowContext) {
    // 获取元信息
    MyAttachment attachment = flowContext.getAttachment();
    attachment.nickname = "渔民小镇";

    // [同步]更新 - 将元信息同步到玩家所在的游戏对外服中
    flowContext.updateAttachment();

    // [异步无阻塞]更新 - 将元信息同步到玩家所在的游戏对外服中
    flowContext.updateAttachmentAsync();
}

class MyFlowContext extends FlowContext {
    MyAttachment attachment;

    @Override
    @SuppressWarnings("unchecked")
    public MyAttachment getAttachment() {
        if (Objects.isNull(attachment)) {
            this.attachment = this.getAttachment(MyAttachment.class);
        }

        return this.attachment;
    }
}