rawTCPSocket.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /**
  2. * @abstract
  3. * @constructor
  4. **/
  5. function AbstractRawTCPSocket() {
  6. /** @type {Array<function(string)>} */
  7. this.onReadyReadHandlers = [];
  8. }
  9. /**
  10. * @abstract
  11. * @return {Promise}
  12. **/
  13. AbstractRawTCPSocket.prototype.connect = function() {};
  14. /**
  15. * @abstract
  16. * @param {string} msg
  17. **/
  18. AbstractRawTCPSocket.prototype.write = function(msg) {};
  19. /**
  20. * @param {function(string)} onReadyRead
  21. **/
  22. AbstractRawTCPSocket.prototype.onReadyRead = function(onReadyRead) {
  23. this.onReadyReadHandlers.push(onReadyRead);
  24. };
  25. /** @param {string} msg */
  26. AbstractRawTCPSocket.prototype.triggerOnReadyRead = function(msg) {
  27. this.onReadyReadHandlers.forEach(function(handler) {
  28. handler(msg);
  29. });
  30. };
  31. AbstractRawTCPSocket.prototype.close = function() {};
  32. //
  33. // This API is not available yet
  34. // https://www.w3.org/TR/raw-sockets/
  35. //
  36. ///**
  37. // * @constructor
  38. // * @extends {AbstractRawTCPSocket}
  39. // * @param {string} address
  40. // * @param {number} port
  41. //**/
  42. //function NavigatorRawTCPSocket(address, port) {
  43. // AbstractRawTCPSocket.apply(this);
  44. //
  45. // /** @type {string} */
  46. // this.address = address;
  47. //
  48. // /** @type {number} */
  49. // this.port = port;
  50. //}
  51. //
  52. //NavigatorRawTCPSocket.prototype = Object.create(AbstractRawTCPSocket);
  53. //NavigatorRawTCPSocket.prototype.constructor = NavigatorRawTCPSocket;
  54. //
  55. //NavigatorRawTCPSocket.prototype.checkPermission = function() {
  56. // return navigator.tcpPermission.requestPermission({ remoteAddress: this.address, remotePort: this.port });
  57. //};
  58. //
  59. //NavigatorRawTCPSocket.prototype.connect = function() {
  60. // this.sock = navigator.TCPSocket(this.address, this.port)
  61. //};
  62. //
  63. //NavigatorRawTCPSocket.prototype.write = function(str) {
  64. //};
  65. //
  66. //NavigatorRawTCPSocket.prototype.close = function() {
  67. //};
  68. /**
  69. * @constructor
  70. * @extends {AbstractRawTCPSocket}
  71. * @param {string} address
  72. * @param {number} port
  73. **/
  74. function NativeRawTCPSocket(address, port) {
  75. AbstractRawTCPSocket.apply(this);
  76. /**
  77. * @const
  78. * @type {number}
  79. **/
  80. this.socketId = NativeRawTCPSocket.sockCount++;
  81. NativeRawTCPSocket.sockets[this.socketId] = this;
  82. /** @type {string} */
  83. this.address = address;
  84. /** @type {number} */
  85. this.port = port;
  86. /** @type {{ onSuccess: function(), onReject: function() }|null} */
  87. this.connectPromise;
  88. }
  89. NativeRawTCPSocket.prototype = Object.create(AbstractRawTCPSocket);
  90. NativeRawTCPSocket.prototype.constructor = NativeRawTCPSocket;
  91. /** @return {Promise} */
  92. NativeRawTCPSocket.prototype.connect = function() {
  93. return new Promise(function(onSuccess, onReject) {
  94. this.connectPromise = {
  95. onSuccess: onSuccess,
  96. onReject: onReject
  97. };
  98. __native.TCPConnect(this.socketId, this.address, this.port);
  99. });
  100. };
  101. /** @param {boolean} success */
  102. NativeRawTCPSocket.prototype.onConnected = function(success) {
  103. if (this.connectPromise) {
  104. if (success)
  105. this.connectPromise.onSuccess();
  106. else
  107. this.connectPromise.onReject();
  108. this.connectPromise = null;
  109. }
  110. };
  111. /** @param {string} msg */
  112. NativeRawTCPSocket.prototype.write = function(msg) {
  113. __native.TCPWrite(this.socketId, msg);
  114. };
  115. NativeRawTCPSocket.prototype.close = function() {
  116. __native.TCPClose(this.socketId);
  117. };
  118. /** @type {number} */
  119. NativeRawTCPSocket.sockCount = 0;
  120. NativeRawTCPSocket.sockets = {};
  121. NativeRawTCPSocket.onConnected = function(sockId, success) {
  122. NativeRawTCPSocket.sockets[sockId].onConnected(success);
  123. };
  124. NativeRawTCPSocket.onReadyRead = function(sockId, msg) {
  125. NativeRawTCPSocket.sockets[sockId].triggerOnReadyRead(msg);
  126. };
  127. /**
  128. * @param {string} address
  129. * @param {number} port
  130. * @return {AbstractRawTCPSocket|null}
  131. **/
  132. function createSocket(address, port) {
  133. if (isNative())
  134. return new NativeRawTCPSocket(address, port);
  135. // else if (navigator && "tcpPermission" in navigator)
  136. // return new NavigatorRawTCPSocket(address, port);
  137. return null; // unsupported
  138. }