| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
const GodotWebSocket = { |
| 32 |
|
| 33 |
$GodotWebSocket__deps: ['$IDHandler', '$GodotRuntime'], |
| 34 |
$GodotWebSocket: { |
| 35 |
|
| 36 |
_onopen: function (p_id, callback, event) { |
| 37 |
const ref = IDHandler.get(p_id); |
| 38 |
if (!ref) { |
| 39 |
return; |
| 40 |
} |
| 41 |
const c_str = GodotRuntime.allocString(ref.protocol); |
| 42 |
callback(c_str); |
| 43 |
GodotRuntime.free(c_str); |
| 44 |
}, |
| 45 |
|
| 46 |
|
| 47 |
_onmessage: function (p_id, callback, event) { |
| 48 |
const ref = IDHandler.get(p_id); |
| 49 |
if (!ref) { |
| 50 |
return; |
| 51 |
} |
| 52 |
let buffer; |
| 53 |
let is_string = 0; |
| 54 |
if (event.data instanceof ArrayBuffer) { |
| 55 |
buffer = new Uint8Array(event.data); |
| 56 |
} else if (event.data instanceof Blob) { |
| 57 |
GodotRuntime.error('Blob type not supported'); |
| 58 |
return; |
| 59 |
} else if (typeof event.data === 'string') { |
| 60 |
is_string = 1; |
| 61 |
buffer = new TextEncoder('utf-8').encode(event.data); |
| 62 |
} else { |
| 63 |
GodotRuntime.error('Unknown message type'); |
| 64 |
return; |
| 65 |
} |
| 66 |
const len = buffer.length * buffer.BYTES_PER_ELEMENT; |
| 67 |
const out = GodotRuntime.malloc(len); |
| 68 |
HEAPU8.set(buffer, out); |
| 69 |
callback(out, len, is_string); |
| 70 |
GodotRuntime.free(out); |
| 71 |
}, |
| 72 |
|
| 73 |
|
| 74 |
_onerror: function (p_id, callback, event) { |
| 75 |
const ref = IDHandler.get(p_id); |
| 76 |
if (!ref) { |
| 77 |
return; |
| 78 |
} |
| 79 |
callback(); |
| 80 |
}, |
| 81 |
|
| 82 |
|
| 83 |
_onclose: function (p_id, callback, event) { |
| 84 |
const ref = IDHandler.get(p_id); |
| 85 |
if (!ref) { |
| 86 |
return; |
| 87 |
} |
| 88 |
const c_str = GodotRuntime.allocString(event.reason); |
| 89 |
callback(event.code, c_str, event.wasClean ? 1 : 0); |
| 90 |
GodotRuntime.free(c_str); |
| 91 |
}, |
| 92 |
|
| 93 |
|
| 94 |
send: function (p_id, p_data) { |
| 95 |
const ref = IDHandler.get(p_id); |
| 96 |
if (!ref || ref.readyState !== ref.OPEN) { |
| 97 |
return 1; |
| 98 |
} |
| 99 |
ref.send(p_data); |
| 100 |
return 0; |
| 101 |
}, |
| 102 |
|
| 103 |
|
| 104 |
bufferedAmount: function (p_id) { |
| 105 |
const ref = IDHandler.get(p_id); |
| 106 |
if (!ref) { |
| 107 |
return 0; |
| 108 |
} |
| 109 |
return ref.bufferedAmount; |
| 110 |
}, |
| 111 |
|
| 112 |
create: function (socket, p_on_open, p_on_message, p_on_error, p_on_close) { |
| 113 |
const id = IDHandler.add(socket); |
| 114 |
socket.onopen = GodotWebSocket._onopen.bind(null, id, p_on_open); |
| 115 |
socket.onmessage = GodotWebSocket._onmessage.bind(null, id, p_on_message); |
| 116 |
socket.onerror = GodotWebSocket._onerror.bind(null, id, p_on_error); |
| 117 |
socket.onclose = GodotWebSocket._onclose.bind(null, id, p_on_close); |
| 118 |
return id; |
| 119 |
}, |
| 120 |
|
| 121 |
|
| 122 |
close: function (p_id, p_code, p_reason) { |
| 123 |
const ref = IDHandler.get(p_id); |
| 124 |
if (ref && ref.readyState < ref.CLOSING) { |
| 125 |
const code = p_code; |
| 126 |
const reason = p_reason; |
| 127 |
ref.close(code, reason); |
| 128 |
} |
| 129 |
}, |
| 130 |
|
| 131 |
|
| 132 |
destroy: function (p_id) { |
| 133 |
const ref = IDHandler.get(p_id); |
| 134 |
if (!ref) { |
| 135 |
return; |
| 136 |
} |
| 137 |
GodotWebSocket.close(p_id, 3001, 'destroyed'); |
| 138 |
IDHandler.remove(p_id); |
| 139 |
ref.onopen = null; |
| 140 |
ref.onmessage = null; |
| 141 |
ref.onerror = null; |
| 142 |
ref.onclose = null; |
| 143 |
}, |
| 144 |
}, |
| 145 |
|
| 146 |
godot_js_websocket_create__proxy: 'sync', |
| 147 |
godot_js_websocket_create__sig: 'iiiiiiii', |
| 148 |
godot_js_websocket_create: function (p_ref, p_url, p_proto, p_on_open, p_on_message, p_on_error, p_on_close) { |
| 149 |
const on_open = GodotRuntime.get_func(p_on_open).bind(null, p_ref); |
| 150 |
const on_message = GodotRuntime.get_func(p_on_message).bind(null, p_ref); |
| 151 |
const on_error = GodotRuntime.get_func(p_on_error).bind(null, p_ref); |
| 152 |
const on_close = GodotRuntime.get_func(p_on_close).bind(null, p_ref); |
| 153 |
const url = GodotRuntime.parseString(p_url); |
| 154 |
const protos = GodotRuntime.parseString(p_proto); |
| 155 |
let socket = null; |
| 156 |
try { |
| 157 |
if (protos) { |
| 158 |
socket = new WebSocket(url, protos.split(',')); |
| 159 |
} else { |
| 160 |
socket = new WebSocket(url); |
| 161 |
} |
| 162 |
} catch (e) { |
| 163 |
return 0; |
| 164 |
} |
| 165 |
socket.binaryType = 'arraybuffer'; |
| 166 |
return GodotWebSocket.create(socket, on_open, on_message, on_error, on_close); |
| 167 |
}, |
| 168 |
|
| 169 |
godot_js_websocket_send__proxy: 'sync', |
| 170 |
godot_js_websocket_send__sig: 'iiiii', |
| 171 |
godot_js_websocket_send: function (p_id, p_buf, p_buf_len, p_raw) { |
| 172 |
const bytes_array = new Uint8Array(p_buf_len); |
| 173 |
let i = 0; |
| 174 |
for (i = 0; i < p_buf_len; i++) { |
| 175 |
bytes_array[i] = GodotRuntime.getHeapValue(p_buf + i, 'i8'); |
| 176 |
} |
| 177 |
let out = bytes_array.buffer; |
| 178 |
if (!p_raw) { |
| 179 |
out = new TextDecoder('utf-8').decode(bytes_array); |
| 180 |
} |
| 181 |
return GodotWebSocket.send(p_id, out); |
| 182 |
}, |
| 183 |
|
| 184 |
godot_js_websocket_buffered_amount__proxy: 'sync', |
| 185 |
godot_js_websocket_buffered_amount__sig: 'ii', |
| 186 |
godot_js_websocket_buffered_amount: function (p_id) { |
| 187 |
return GodotWebSocket.bufferedAmount(p_id); |
| 188 |
}, |
| 189 |
|
| 190 |
godot_js_websocket_close__proxy: 'sync', |
| 191 |
godot_js_websocket_close__sig: 'viii', |
| 192 |
godot_js_websocket_close: function (p_id, p_code, p_reason) { |
| 193 |
const code = p_code; |
| 194 |
const reason = GodotRuntime.parseString(p_reason); |
| 195 |
GodotWebSocket.close(p_id, code, reason); |
| 196 |
}, |
| 197 |
|
| 198 |
godot_js_websocket_destroy__proxy: 'sync', |
| 199 |
godot_js_websocket_destroy__sig: 'vi', |
| 200 |
godot_js_websocket_destroy: function (p_id) { |
| 201 |
GodotWebSocket.destroy(p_id); |
| 202 |
}, |
| 203 |
}; |
| 204 |
|
| 205 |
autoAddDeps(GodotWebSocket, '$GodotWebSocket'); |
| 206 |
mergeInto(LibraryManager.library, GodotWebSocket); |