library_godot_websocket.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*************************************************************************/
  2. /* library_godot_websocket.js */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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. create: function (socket, p_on_open, p_on_message, p_on_error, p_on_close) {
  99. const id = IDHandler.add(socket);
  100. socket.onopen = GodotWebSocket._onopen.bind(null, id, p_on_open);
  101. socket.onmessage = GodotWebSocket._onmessage.bind(null, id, p_on_message);
  102. socket.onerror = GodotWebSocket._onerror.bind(null, id, p_on_error);
  103. socket.onclose = GodotWebSocket._onclose.bind(null, id, p_on_close);
  104. return id;
  105. },
  106. // Closes the JavaScript WebSocket (if not already closing) associated to a given C++ object.
  107. close: function (p_id, p_code, p_reason) {
  108. const ref = IDHandler.get(p_id);
  109. if (ref && ref.readyState < ref.CLOSING) {
  110. const code = p_code;
  111. const reason = GodotRuntime.parseString(p_reason);
  112. ref.close(code, reason);
  113. }
  114. },
  115. // Deletes the reference to a C++ object (closing any connected socket if necessary).
  116. destroy: function (p_id) {
  117. const ref = IDHandler.get(p_id);
  118. if (!ref) {
  119. return;
  120. }
  121. GodotWebSocket.close(p_id, 1001, '');
  122. IDHandler.remove(p_id);
  123. ref.onopen = null;
  124. ref.onmessage = null;
  125. ref.onerror = null;
  126. ref.onclose = null;
  127. },
  128. },
  129. godot_js_websocket_create__sig: 'iiiiiiii',
  130. godot_js_websocket_create: function (p_ref, p_url, p_proto, p_on_open, p_on_message, p_on_error, p_on_close) {
  131. const on_open = GodotRuntime.get_func(p_on_open).bind(null, p_ref);
  132. const on_message = GodotRuntime.get_func(p_on_message).bind(null, p_ref);
  133. const on_error = GodotRuntime.get_func(p_on_error).bind(null, p_ref);
  134. const on_close = GodotRuntime.get_func(p_on_close).bind(null, p_ref);
  135. const url = GodotRuntime.parseString(p_url);
  136. const protos = GodotRuntime.parseString(p_proto);
  137. let socket = null;
  138. try {
  139. if (protos) {
  140. socket = new WebSocket(url, protos.split(','));
  141. } else {
  142. socket = new WebSocket(url);
  143. }
  144. } catch (e) {
  145. return 0;
  146. }
  147. socket.binaryType = 'arraybuffer';
  148. return GodotWebSocket.create(socket, on_open, on_message, on_error, on_close);
  149. },
  150. godot_js_websocket_send__sig: 'iiiii',
  151. godot_js_websocket_send: function (p_id, p_buf, p_buf_len, p_raw) {
  152. const bytes_array = new Uint8Array(p_buf_len);
  153. let i = 0;
  154. for (i = 0; i < p_buf_len; i++) {
  155. bytes_array[i] = GodotRuntime.getHeapValue(p_buf + i, 'i8');
  156. }
  157. let out = bytes_array.buffer;
  158. if (!p_raw) {
  159. out = new TextDecoder('utf-8').decode(bytes_array);
  160. }
  161. return GodotWebSocket.send(p_id, out);
  162. },
  163. godot_js_websocket_close__sig: 'viii',
  164. godot_js_websocket_close: function (p_id, p_code, p_reason) {
  165. const code = p_code;
  166. const reason = GodotRuntime.parseString(p_reason);
  167. GodotWebSocket.close(p_id, code, reason);
  168. },
  169. godot_js_websocket_destroy__sig: 'vi',
  170. godot_js_websocket_destroy: function (p_id) {
  171. GodotWebSocket.destroy(p_id);
  172. },
  173. };
  174. autoAddDeps(GodotWebSocket, '$GodotWebSocket');
  175. mergeInto(LibraryManager.library, GodotWebSocket);