BestHTTP_EventSource.jslib 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. var Lib_BEST_HTTP_WebGL_ES_Bridge =
  2. {
  3. $es: {
  4. eventSourceInstances: {},
  5. nextInstanceId : 1,
  6. Set : function(event) {
  7. es.eventSourceInstances[es.nextInstanceId] = event;
  8. return es.nextInstanceId++;
  9. },
  10. Get : function(id) {
  11. return es.eventSourceInstances[id];
  12. },
  13. Remove: function(id) {
  14. delete es.eventSourceInstances[id];
  15. },
  16. _callOnError: function(errCallback, id, reason)
  17. {
  18. if (reason)
  19. {
  20. var length = lengthBytesUTF8(reason) + 1;
  21. var buffer = _malloc(length);
  22. stringToUTF8Array(reason, HEAPU8, buffer, length);
  23. Runtime.dynCall('vii', errCallback, [id, buffer]);
  24. _free(buffer);
  25. }
  26. else
  27. Runtime.dynCall('vii', errCallback, [id, 0]);
  28. },
  29. _GenericEventHandler: function(id, eventName, e, onMessage) {
  30. function AllocString(str) {
  31. if (str != undefined)
  32. {
  33. var length = lengthBytesUTF8(str) + 1;
  34. var buff = _malloc(length);
  35. stringToUTF8Array(str, HEAPU8, buff, length);
  36. return buff;
  37. }
  38. return 0;
  39. }
  40. var eventBuffer = AllocString(eventName);
  41. var dataBuffer = AllocString(e.data);
  42. var idBuffer = AllocString(e.id);
  43. Runtime.dynCall('viiiii', onMessage, [id, eventBuffer, dataBuffer, idBuffer, e.retry]);
  44. if (eventBuffer != 0)
  45. _free(eventBuffer);
  46. if (dataBuffer != 0)
  47. _free(dataBuffer);
  48. if (idBuffer != 0)
  49. _free(idBuffer);
  50. }
  51. },
  52. ES_IsSupported: function() {
  53. return typeof(EventSource) !== "undefined";
  54. },
  55. ES_Create: function(urlPtr, withCredentials, onOpen, onMessage, onError)
  56. {
  57. var url = /*encodeURI*/(Pointer_stringify(urlPtr))
  58. .replace(/\+/g, '%2B')
  59. .replace(/%252[fF]/ig, '%2F');
  60. var event = {
  61. onError: onError
  62. };
  63. var id = es.nextInstanceId;
  64. console.log(id + ' ES_Create(' + url + ', ' + withCredentials + ')');
  65. event.eventImpl = new EventSource(url, { withCredentials: withCredentials != 0 ? true : false } );
  66. event.onMessage = onMessage;
  67. event.eventImpl.onopen = function() {
  68. console.log(id + ' ES_Create - onOpen');
  69. Runtime.dynCall('vi', onOpen, [id]);
  70. };
  71. event.eventImpl.onmessage = function(e) {
  72. console.log(id + ' on Generic Message');
  73. es._GenericEventHandler(id, undefined, e, onMessage);
  74. };
  75. event.eventImpl.onerror = function(e) {
  76. console.log(id + ' ES_Create - onError');
  77. es._callOnError(onError, id, "Unknown Error!");
  78. };
  79. return es.Set(event);
  80. },
  81. ES_AddEventHandler: function(id, eventNamePtr) {
  82. var eventName = Pointer_stringify(eventNamePtr);
  83. console.log(id + ' ES_AddEventHandler(' + eventName + ')');
  84. var event = es.Get(id);
  85. try
  86. {
  87. event.eventImpl.addEventListener(eventName, function(e) {
  88. console.log(id + ' onEvent('+ eventName + ')');
  89. es._GenericEventHandler(id, eventName, e, event.onMessage);
  90. });
  91. }
  92. catch(e) {
  93. es._callOnError(event.eventImpl.onError, id, ' ' + e.name + ': ' + e.message);
  94. }
  95. },
  96. ES_Close: function(id)
  97. {
  98. console.log(id + ' ES_Close');
  99. var event = es.Get(id);
  100. try
  101. {
  102. event.eventImpl.close();
  103. }
  104. catch(e) {
  105. es._callOnError(event.eventImpl.onError, id, ' ' + e.name + ': ' + e.message);
  106. }
  107. },
  108. ES_Release: function(id)
  109. {
  110. console.log(id + ' ES_Release');
  111. es.Remove(id);
  112. }
  113. };
  114. autoAddDeps(Lib_BEST_HTTP_WebGL_ES_Bridge, '$es');
  115. mergeInto(LibraryManager.library, Lib_BEST_HTTP_WebGL_ES_Bridge);