MCPcopy Create free account
hub / github.com/esmBot/esmBot / onMessage

Method onMessage

src/utils/mediaConnection.ts:79–123  ·  view source on GitHub ↗
(msg: Data)

Source from the content-addressed store, hash-verified

77 }
78
79 onMessage(msg: Data) {
80 if (!(msg instanceof Buffer)) return;
81 const op = msg.readUint8(0);
82 logger.debug(`Received message from media server ${this.host} with opcode ${op}`);
83 if (op === Rinit) {
84 this.formats = JSON.parse(msg.toString("utf8", 7));
85 this.funcs = {
86 image: this.formats.image ? Object.keys(this.formats.image) : [],
87 };
88 this.types = {};
89 for (const [type, cmdList] of Object.entries(this.formats)) {
90 const cmds = Object.keys(cmdList);
91 for (const cmd of cmds) {
92 if (!this.types[cmd]) this.types[cmd] = [];
93 this.types[cmd].push(type as MediaTypes);
94 }
95 }
96 return;
97 }
98 if (op === Rclose) {
99 this.reconnect = true;
100 this.close();
101 return;
102 }
103 const tag = msg.readUint16LE(1);
104 const promise = this.requests.get(tag);
105 if (!promise) {
106 logger.error(`Received response for unknown request ${tag}`);
107 return;
108 }
109 this.requests.delete(tag);
110 if (op === Rerror) {
111 promise.reject(new Error(msg.subarray(3, msg.length).toString()));
112 return;
113 }
114 if (op === Rsent) {
115 promise.resolve({ sent: true, data: msg.subarray(3, msg.length) });
116 return;
117 }
118 if (op === Rwait) {
119 promise.resolve({ sent: false });
120 return;
121 }
122 promise.resolve();
123 }
124
125 onError(e: Error | ErrorEvent) {
126 logger.error(e);

Callers 2

constructorMethod · 0.95
onCloseMethod · 0.95

Calls 2

closeMethod · 0.95
deleteMethod · 0.45

Tested by

no test coverage detected