Debian Codegraph @

modules/websocket/library_godot_websocket.js

raw symbols SCIP
1 /**************************************************************************/
2 /* library_godot_websocket.js */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 const GodotWebSocket = {
32 // Our socket implementation that forwards events to C++.
33 $GodotWebSocket__deps: ['$IDHandler', '$GodotRuntime'],
34 $GodotWebSocket: {
35 // Connection opened, report selected protocol
36 _onopen: function (p_id, callback, event) {
37 const ref = IDHandler.get(p_id);
38 if (!ref) {
39 return; // Godot object is gone.
40 }
41 const c_str = GodotRuntime.allocString(ref.protocol);
42 callback(c_str);
43 GodotRuntime.free(c_str);
44 },
45
46 // Message received, report content and type (UTF8 vs binary)
47 _onmessage: function (p_id, callback, event) {
48 const ref = IDHandler.get(p_id);
49 if (!ref) {
50 return; // Godot object is gone.
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 // An error happened, 'onclose' will be called after this.
74 _onerror: function (p_id, callback, event) {
75 const ref = IDHandler.get(p_id);
76 if (!ref) {
77 return; // Godot object is gone.
78 }
79 callback();
80 },
81
82 // Connection is closed, this is always fired. Report close code, reason, and clean status.
83 _onclose: function (p_id, callback, event) {
84 const ref = IDHandler.get(p_id);
85 if (!ref) {
86 return; // Godot object is gone.
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 // Send a message
94 send: function (p_id, p_data) {
95 const ref = IDHandler.get(p_id);
96 if (!ref || ref.readyState !== ref.OPEN) {
97 return 1; // Godot object is gone or socket is not in a ready state.
98 }
99 ref.send(p_data);
100 return 0;
101 },
102
103 // Get current bufferedAmount
104 bufferedAmount: function (p_id) {
105 const ref = IDHandler.get(p_id);
106 if (!ref) {
107 return 0; // Godot object is gone.
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 // Closes the JavaScript WebSocket (if not already closing) associated to a given C++ object.
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 // Deletes the reference to a C++ object (closing any connected socket if necessary).
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);