Skip to main content

Meta Information - Attachment

Introduction

When processing actions, FlowContext can easily provide current userId.

If developers want to carry custom information during action processing, this can be done through meta-attachment capability. After storing custom data in attachment, every subsequent action request can obtain these custom fields via FlowContext.

For example, you can store current player's hero role info, player nickname, or use this feature to adapt projects where userId is string or another type. In short, if you want player-specific data in FlowContext, this feature provides it.

Example Source Code

see https://github.com/iohao/ionet-examples

path : ionet-cookbook-code

  • AttachmentAction

How to Use

Custom attachment class

@ProtobufClass
public class MyAttachment {
public long userId;
public String nickname;
}

Update attachment

  • code 10~11: set and synchronously update attachment.
  • code 20: get attachment.
@ActionController(AttachmentCmd.cmd)
public class AttachmentAction {
@ActionMethod(AttachmentCmd.updateAttachment)
private boolean updateAttachment(MyFlowContext flowContext) {
var attachment = new MyAttachment();
attachment.userId = flowContext.getUserId();
attachment.nickname = "Michael Jackson";

// set and update attachment
flowContext.setAttachment(attachment);
flowContext.updateAttachment();
// Async update
// flowContext.updateAttachmentAsync();

return true;
}

@ActionMethod(AttachmentCmd.printAttachment)
private String printAttachment(FlowContext flowContext) {
MyAttachment attachment = flowContext.getAttachment();
return attachment.toString();
}
}

Coordination with Custom FlowContext

We can combine this with custom FlowContext for more tailored extensions, as shown below.

  • code 5: call MyFlowContext.getNickname().
  • code 10: custom MyFlowContext; any convenience methods can be added in this class.
@ActionController(1)
public class DemoAction {
@ActionMethod(1)
public String getNickname(MyFlowContext flowContext) {
return flowContext.getNickname();
}
}

@Setter
public class MyFlowContext extends DefaultFlowContext {
MyAttachment attachment;

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

return this.attachment;
}

public String getNickname() {
MyAttachment attachment = this.getAttachment();
return attachment.nickname;
}
}