MCPcopy Create free account
hub / github.com/Acode-Foundation/Acode / send

Method send

src/plugins/websocket/www/websocket.js:98–122  ·  view source on GitHub ↗
(message, binary)

Source from the content-addressed store, hash-verified

96
97
98 send(message, binary) {
99 if (this.readyState !== WebSocketInstance.OPEN) {
100 throw new Error(`WebSocket is not open/connected`);
101 }
102
103 let finalMessage = null;
104 if (message instanceof ArrayBuffer || ArrayBuffer.isView(message)) {
105 const uint8Array = message instanceof ArrayBuffer ? new Uint8Array(message) : message;
106 finalMessage = btoa(String.fromCharCode.apply(null, uint8Array));
107
108 // set to true as it's the data of a binary (type 0x2) message.
109 binary = true;
110
111 exec(() => logIfDebug(`[Cordova WebSocket - ID=${this.instanceId}] Sent message(binary payload):`, finalMessage), (err) => console.error(`[Cordova WebSocket - ID=${this.instanceId}] Send error:`, err), "WebSocketPlugin", "send", [this.instanceId, finalMessage, binary]);
112 } else if (typeof message === 'string') {
113 finalMessage = message;
114
115 // maybe a String to be sent as Binary (if it's true)
116 if(binary) finalMessage = btoa(message)
117
118 exec(() => logIfDebug(`[Cordova WebSocket - ID=${this.instanceId}] Sent message(binary=${binary}):`, finalMessage), (err) => console.error(`[Cordova WebSocket - ID=${this.instanceId}] Send error:`, err), "WebSocketPlugin", "send", [this.instanceId, finalMessage, binary]);
119 } else {
120 throw new Error(`Unsupported message type: ${typeof message}`);
121 }
122 }
123
124 /**
125 * Closes the WebSocket connection.

Callers

nothing calls this directly

Calls 3

logIfDebugFunction · 0.85
execFunction · 0.50
errorMethod · 0.45

Tested by

no test coverage detected