Skip to content

How do you transmit raw byte data in Javascript/Typescript? #7097

Answered by sketch34
sketch34 asked this question in Q&A
Discussion options

You must be logged in to vote

I forgot to post final solution (to "update 2") once I figured it out:

union Message {
  MyMessageType,
  ...
}


table MyRootMsg {
  msg: Message;  // Message is a union.
}

flatc will generate this as part of the MyRootMsg type:

msg<T extends flatbuffers.Table>(obj:any):any|null {
  const offset = this.bb!.__offset(this.bb_pos, 6);
  return offset ? this.bb!.__union(obj, this.bb_pos + offset) : null;
}

which we can use as the "accessor":

let root_msg = MyRootMsg.getSizePrefixedRootAsMyRootMsg(new flatbuffers.ByteBuffer(data));

// Must bind to set 'this' correctly for function object.
let accessor = root_msg.msg.bind(root_msg);

let m = unionToMessage(Message.MyMessageType, accessor) a…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by sketch34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant