library_godot_websocket.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. const GodotWebSocket = {
  31. // Our socket implementation that forwards events to C++.
  32. $GodotWebSocket__deps: ['$IDHandler', '$GodotRuntime'],
  33. $GodotWebSocket: {
  34. // Connection opened, report selected protocol
  35. _onopen: function (p_id, callback, event) {
  36. const ref = IDHandler.get(p_id);
  37. if (!ref) {
  38. return; // Godot object is gone.
  39. }
  40. const c_str = GodotRuntime.allocString(ref.protocol);
  41. callback(c_str);
  42. GodotRuntime.free(c_str);
  43. },
  44. // Message received, report content and type (UTF8 vs binary)
  45. _onmessage: function (p_id, callback, event) {
  46. const ref = IDHandler.get(p_id);
  47. if (!ref) {
  48. return; // Godot object is gone.
  49. }
  50. let buffer;
  51. let is_string = 0;
  52. if (event.data instanceof ArrayBuffer) {
  53. buffer = new Uint8Array(event.data);
  54. } else if (event.data instanceof Blob) {
  55. GodotRuntime.error('Blob type not supported');
  56. return;
  57. } else if (typeof event.data === 'string') {
  58. is_string = 1;
  59. const enc = new TextEncoder('utf-8');
  60. buffer = new Uint8Array(enc.encode(event.data));
  61. } else {
  62. GodotRuntime.error('Unknown message type');
  63. return;
  64. }
  65. const len = buffer.length * buffer.BYTES_PER_ELEMENT;
  66. const out = GodotRuntime.malloc(len);
  67. HEAPU8.set(buffer, out);
  68. callback(out, len, is_string);
  69. GodotRuntime.free(out);
  70. },
  71. // An error happened, 'onclose' will be called after this.
  72. _onerror: function (p_id, callback, event) {
  73. const ref = IDHandler.get(p_id);
  74. if (!ref) {
  75. return; // Godot object is gone.
  76. }
  77. callback();
  78. },
  79. // Connection is closed, this is always fired. Report close code, reason, and clean status.
  80. _onclose: function (p_id, callback, event) {
  81. const ref = IDHandler.get(p_id);
  82. if (!ref) {
  83. return; // Godot object is gone.
  84. }
  85. const c_str = GodotRuntime.allocString(event.reason);
  86. callback(event.code, c_str, event.wasClean ? 1 : 0);
  87. GodotRuntime.free(c_str);
  88. },
  89. // Send a message
  90. send: function (p_id, p_data) {
  91. const ref = IDHandler.get(p_id);
  92. if (!ref || ref.readyState !== ref.OPEN) {
  93. return 1; // Godot object is gone or socket is not in a ready state.
  94. }
  95. ref.send(p_data);
  96. return 0;
  97. },
  98. // Get current bufferedAmount
  99. bufferedAmount: function (p_id) {
  100. const ref = IDHandler.get(p_id);
  101. if (!ref) {
  102. return 0; // Godot object is gone.
  103. }
  104. return ref.bufferedAmount;
  105. },
  106. create: function (socket, p_on_open, p_on_message, p_on_error, p_on_close) {
  107. const id = IDHandler.add(socket);
  108. socket.onopen = GodotWebSocket._onopen.bind(null, id, p_on_open);
  109. socket.onmessage = GodotWebSocket._onmessage.bind(null, id, p_on_message);
  110. socket.onerror = GodotWebSocket._onerror.bind(null, id, p_on_error);
  111. socket.onclose = GodotWebSocket._onclose.bind(null, id, p_on_close);
  112. return id;
  113. },
  114. // Closes the JavaScript WebSocket (if not already closing) associated to a given C++ object.
  115. close: function (p_id, p_code, p_reason) {
  116. const ref = IDHandler.get(p_id);
  117. if (ref && ref.readyState < ref.CLOSING) {
  118. const code = p_code;
  119. const reason = GodotRuntime.parseString(p_reason);
  120. ref.close(code, reason);
  121. }
  122. },
  123. // Deletes the reference to a C++ object (closing any connected socket if necessary).
  124. destroy: function (p_id) {
  125. const ref = IDHandler.get(p_id);
  126. if (!ref) {
  127. return;
  128. }
  129. GodotWebSocket.close(p_id, 3001, 'destroyed');
  130. IDHandler.remove(p_id);
  131. ref.onopen = null;
  132. ref.onmessage = null;
  133. ref.onerror = null;
  134. ref.onclose = null;
  135. },
  136. },
  137. godot_js_websocket_create__sig: 'iiiiiiii',
  138. godot_js_websocket_create: function (p_ref, p_url, p_proto, p_on_open, p_on_message, p_on_error, p_on_close) {
  139. const on_open = GodotRuntime.get_func(p_on_open).bind(null, p_ref);
  140. const on_message = GodotRuntime.get_func(p_on_message).bind(null, p_ref);
  141. const on_error = GodotRuntime.get_func(p_on_error).bind(null, p_ref);
  142. const on_close = GodotRuntime.get_func(p_on_close).bind(null, p_ref);
  143. const url = GodotRuntime.parseString(p_url);
  144. const protos = GodotRuntime.parseString(p_proto);
  145. let socket = null;
  146. try {
  147. if (protos) {
  148. socket = new WebSocket(url, protos.split(','));
  149. } else {
  150. socket = new WebSocket(url);
  151. }
  152. } catch (e) {
  153. return 0;
  154. }
  155. socket.binaryType = 'arraybuffer';
  156. return GodotWebSocket.create(socket, on_open, on_message, on_error, on_close);
  157. },
  158. godot_js_websocket_send__sig: 'iiiii',
  159. godot_js_websocket_send: function (p_id, p_buf, p_buf_len, p_raw) {
  160. const bytes_array = new Uint8Array(p_buf_len);
  161. let i = 0;
  162. for (i = 0; i < p_buf_len; i++) {
  163. bytes_array[i] = GodotRuntime.getHeapValue(p_buf + i, 'i8');
  164. }
  165. let out = bytes_array.buffer;
  166. if (!p_raw) {
  167. out = new TextDecoder('utf-8').decode(bytes_array);
  168. }
  169. return GodotWebSocket.send(p_id, out);
  170. },
  171. godot_js_websocket_buffered_amount__sig: 'ii',
  172. godot_js_websocket_buffered_amount: function (p_id) {
  173. return GodotWebSocket.bufferedAmount(p_id);
  174. },
  175. godot_js_websocket_close__sig: 'viii',
  176. godot_js_websocket_close: function (p_id, p_code, p_reason) {
  177. const code = p_code;
  178. const reason = GodotRuntime.parseString(p_reason);
  179. GodotWebSocket.close(p_id, code, reason);
  180. },
  181. godot_js_websocket_destroy__sig: 'vi',
  182. godot_js_websocket_destroy: function (p_id) {
  183. GodotWebSocket.destroy(p_id);
  184. },
  185. };
  186. autoAddDeps(GodotWebSocket, '$GodotWebSocket');
  187. mergeInto(LibraryManager.library, GodotWebSocket);