WebSocket.jslib 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. var WebSocketLibrary =
  2. {
  3. $webSocketManager:
  4. {
  5. /*
  6. * Map of instances
  7. *
  8. * Instance structure:
  9. * {
  10. * url: string,
  11. * ws: WebSocket,
  12. * subProtocols: string[],
  13. * }
  14. */
  15. instances: {},
  16. /* Last instance ID */
  17. lastId: 0,
  18. /* Event listeners */
  19. onOpen: null,
  20. onMessage: null,
  21. onMessageStr: null,
  22. onError: null,
  23. onClose: null
  24. },
  25. /**
  26. * Set onOpen callback
  27. *
  28. * @param callback Reference to C# static function
  29. */
  30. WebSocketSetOnOpen: function(callback)
  31. {
  32. webSocketManager.onOpen = callback;
  33. },
  34. /**
  35. * Set onMessage callback
  36. *
  37. * @param callback Reference to C# static function
  38. */
  39. WebSocketSetOnMessage: function(callback)
  40. {
  41. webSocketManager.onMessage = callback;
  42. },
  43. /**
  44. * Set onMessageStr callback
  45. *
  46. * @param callback Reference to C# static function
  47. */
  48. WebSocketSetOnMessageStr: function(callback)
  49. {
  50. webSocketManager.onMessageStr = callback;
  51. },
  52. /**
  53. * Set onError callback
  54. *
  55. * @param callback Reference to C# static function
  56. */
  57. WebSocketSetOnError: function(callback)
  58. {
  59. webSocketManager.onError = callback;
  60. },
  61. /**
  62. * Set onClose callback
  63. *
  64. * @param callback Reference to C# static function
  65. */
  66. WebSocketSetOnClose: function(callback)
  67. {
  68. webSocketManager.onClose = callback;
  69. },
  70. /**
  71. * Allocate new WebSocket instance struct
  72. *
  73. * @param url Server URL
  74. */
  75. WebSocketAllocate: function(urlPtr)
  76. {
  77. var url = UTF8ToString(urlPtr);
  78. var id = ++webSocketManager.lastId;
  79. webSocketManager.instances[id] = {
  80. url: url,
  81. ws: null,
  82. };
  83. return id;
  84. },
  85. /**
  86. * Add Sub Protocol
  87. *
  88. * @param instanceId Instance ID
  89. * @param protocol Sub Protocol
  90. */
  91. WebSocketAddSubProtocol: function(instanceId, protocolPtr)
  92. {
  93. var instance = webSocketManager.instances[instanceId];
  94. if (!instance) return -1;
  95. var protocol = UTF8ToString(protocolPtr);
  96. if (instance.subProtocols == null)
  97. instance.subProtocols = [];
  98. instance.subProtocols.push(protocol);
  99. return 0;
  100. },
  101. /**
  102. * Remove reference to WebSocket instance
  103. *
  104. * If socket is not closed function will close it but onClose event will not be emitted because
  105. * this function should be invoked by C# WebSocket destructor.
  106. *
  107. * @param instanceId Instance ID
  108. */
  109. WebSocketFree: function(instanceId)
  110. {
  111. var instance = webSocketManager.instances[instanceId];
  112. if (!instance) return 0;
  113. // Close if not closed
  114. if (instance.ws !== null && instance.ws.readyState < 2)
  115. instance.ws.close();
  116. // Remove reference
  117. delete webSocketManager.instances[instanceId];
  118. return 0;
  119. },
  120. /**
  121. * Connect WebSocket to the server
  122. *
  123. * @param instanceId Instance ID
  124. */
  125. WebSocketConnect: function(instanceId)
  126. {
  127. var instance = webSocketManager.instances[instanceId];
  128. if (!instance) return -1;
  129. if (instance.ws !== null) return -2;
  130. if (instance.subProtocols != null)
  131. instance.ws = new WebSocket(instance.url, instance.subProtocols);
  132. else
  133. instance.ws = new WebSocket(instance.url);
  134. instance.ws.onopen = function()
  135. {
  136. Module.dynCall_vi(webSocketManager.onOpen, instanceId);
  137. };
  138. instance.ws.onmessage = function(ev)
  139. {
  140. if (ev.data instanceof ArrayBuffer)
  141. {
  142. var array = new Uint8Array(ev.data);
  143. var buffer = _malloc(array.length);
  144. writeArrayToMemory(array, buffer);
  145. try
  146. {
  147. Module.dynCall_viii(webSocketManager.onMessage, instanceId, buffer, array.length);
  148. }
  149. finally
  150. {
  151. _free(buffer);
  152. }
  153. }
  154. else if (typeof ev.data == 'string')
  155. {
  156. var length = lengthBytesUTF8(ev.data) + 1;
  157. var buffer = _malloc(length);
  158. stringToUTF8(ev.data, buffer, length);
  159. try
  160. {
  161. Module.dynCall_vii(webSocketManager.onMessageStr, instanceId, buffer);
  162. }
  163. finally
  164. {
  165. _free(buffer);
  166. }
  167. }
  168. else if (typeof Blob !== 'undefined' && ev.data instanceof Blob)
  169. {
  170. var reader = new FileReader();
  171. reader.onload = function()
  172. {
  173. var array = new Uint8Array(reader.result);
  174. var buffer = _malloc(array.length);
  175. writeArrayToMemory(array, buffer);
  176. try
  177. {
  178. Module.dynCall_viii(webSocketManager.onMessage, instanceId, buffer, array.length);
  179. }
  180. finally
  181. {
  182. reader = null;
  183. _free(buffer);
  184. }
  185. };
  186. reader.readAsArrayBuffer(ev.data);
  187. }
  188. else
  189. {
  190. console.log("[JSLIB WebSocket] not support message type: ", (typeof ev.data));
  191. }
  192. };
  193. instance.ws.onerror = function(ev)
  194. {
  195. var msg = "WebSocket error.";
  196. var length = lengthBytesUTF8(msg) + 1;
  197. var buffer = _malloc(length);
  198. stringToUTF8(msg, buffer, length);
  199. try
  200. {
  201. Module.dynCall_vii(webSocketManager.onError, instanceId, buffer);
  202. }
  203. finally
  204. {
  205. _free(buffer);
  206. }
  207. };
  208. instance.ws.onclose = function(ev)
  209. {
  210. var msg = ev.reason;
  211. var length = lengthBytesUTF8(msg) + 1;
  212. var buffer = _malloc(length);
  213. stringToUTF8(msg, buffer, length);
  214. try
  215. {
  216. Module.dynCall_viii(webSocketManager.onClose, instanceId, ev.code, buffer);
  217. }
  218. finally
  219. {
  220. _free(buffer);
  221. }
  222. instance.ws = null;
  223. };
  224. return 0;
  225. },
  226. /**
  227. * Close WebSocket connection
  228. *
  229. * @param instanceId Instance ID
  230. * @param code Close status code
  231. * @param reasonPtr Pointer to reason string
  232. */
  233. WebSocketClose: function(instanceId, code, reasonPtr)
  234. {
  235. var instance = webSocketManager.instances[instanceId];
  236. if (!instance) return -1;
  237. if (instance.ws === null) return -3;
  238. if (instance.ws.readyState === 2) return -4;
  239. if (instance.ws.readyState === 3) return -5;
  240. var reason = ( reasonPtr ? UTF8ToString(reasonPtr) : undefined );
  241. try
  242. {
  243. instance.ws.close(code, reason);
  244. }
  245. catch (err)
  246. {
  247. return -7;
  248. }
  249. return 0;
  250. },
  251. /**
  252. * Send message over WebSocket
  253. *
  254. * @param instanceId Instance ID
  255. * @param bufferPtr Pointer to the message buffer
  256. * @param length Length of the message in the buffer
  257. */
  258. WebSocketSend: function(instanceId, bufferPtr, length)
  259. {
  260. var instance = webSocketManager.instances[instanceId];
  261. if (!instance) return -1;
  262. if (instance.ws === null) return -3;
  263. if (instance.ws.readyState !== 1) return -6;
  264. if (typeof HEAPU8 !== 'undefined')
  265. instance.ws.send(HEAPU8.buffer.slice(bufferPtr, bufferPtr + length));
  266. else if (typeof buffer !== 'undefined')
  267. instance.ws.send(buffer.slice(bufferPtr, bufferPtr + length));
  268. else
  269. return -8; // not support buffer slice
  270. return 0;
  271. },
  272. /**
  273. * Send message string over WebSocket
  274. *
  275. * @param instanceId Instance ID
  276. * @param stringPtr Pointer to the message string
  277. */
  278. WebSocketSendStr: function(instanceId, stringPtr)
  279. {
  280. var instance = webSocketManager.instances[instanceId];
  281. if (!instance) return -1;
  282. if (instance.ws === null) return -3;
  283. if (instance.ws.readyState !== 1) return -6;
  284. instance.ws.send(UTF8ToString(stringPtr));
  285. return 0;
  286. },
  287. /**
  288. * Return WebSocket readyState
  289. *
  290. * @param instanceId Instance ID
  291. */
  292. WebSocketGetState: function(instanceId)
  293. {
  294. var instance = webSocketManager.instances[instanceId];
  295. if (!instance) return -1;
  296. if (instance.ws === null) return 3; // socket null as closed
  297. return instance.ws.readyState;
  298. }
  299. };
  300. autoAddDeps(WebSocketLibrary, '$webSocketManager');
  301. mergeInto(LibraryManager.library, WebSocketLibrary);