Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

client.js 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Client = void 0;
  4. const socket_io_parser_1 = require("socket.io-parser");
  5. const debugModule = require("debug");
  6. const url = require("url");
  7. const debug = debugModule("socket.io:client");
  8. class Client {
  9. /**
  10. * Client constructor.
  11. *
  12. * @param server instance
  13. * @param conn
  14. * @package
  15. */
  16. constructor(server, conn) {
  17. this.sockets = new Map();
  18. this.nsps = new Map();
  19. this.server = server;
  20. this.conn = conn;
  21. this.encoder = server.encoder;
  22. this.decoder = new server._parser.Decoder();
  23. this.id = conn.id;
  24. this.setup();
  25. }
  26. /**
  27. * @return the reference to the request that originated the Engine.IO connection
  28. *
  29. * @public
  30. */
  31. get request() {
  32. return this.conn.request;
  33. }
  34. /**
  35. * Sets up event listeners.
  36. *
  37. * @private
  38. */
  39. setup() {
  40. this.onclose = this.onclose.bind(this);
  41. this.ondata = this.ondata.bind(this);
  42. this.onerror = this.onerror.bind(this);
  43. this.ondecoded = this.ondecoded.bind(this);
  44. // @ts-ignore
  45. this.decoder.on("decoded", this.ondecoded);
  46. this.conn.on("data", this.ondata);
  47. this.conn.on("error", this.onerror);
  48. this.conn.on("close", this.onclose);
  49. this.connectTimeout = setTimeout(() => {
  50. if (this.nsps.size === 0) {
  51. debug("no namespace joined yet, close the client");
  52. this.close();
  53. }
  54. else {
  55. debug("the client has already joined a namespace, nothing to do");
  56. }
  57. }, this.server._connectTimeout);
  58. }
  59. /**
  60. * Connects a client to a namespace.
  61. *
  62. * @param {String} name - the namespace
  63. * @param {Object} auth - the auth parameters
  64. * @private
  65. */
  66. connect(name, auth = {}) {
  67. if (this.server._nsps.has(name)) {
  68. debug("connecting to namespace %s", name);
  69. return this.doConnect(name, auth);
  70. }
  71. this.server._checkNamespace(name, auth, (dynamicNspName) => {
  72. if (dynamicNspName) {
  73. debug("dynamic namespace %s was created", dynamicNspName);
  74. this.doConnect(name, auth);
  75. }
  76. else {
  77. debug("creation of namespace %s was denied", name);
  78. this._packet({
  79. type: socket_io_parser_1.PacketType.CONNECT_ERROR,
  80. nsp: name,
  81. data: {
  82. message: "Invalid namespace",
  83. },
  84. });
  85. }
  86. });
  87. }
  88. /**
  89. * Connects a client to a namespace.
  90. *
  91. * @param name - the namespace
  92. * @param {Object} auth - the auth parameters
  93. *
  94. * @private
  95. */
  96. doConnect(name, auth) {
  97. const nsp = this.server.of(name);
  98. const socket = nsp._add(this, auth, () => {
  99. this.sockets.set(socket.id, socket);
  100. this.nsps.set(nsp.name, socket);
  101. if (this.connectTimeout) {
  102. clearTimeout(this.connectTimeout);
  103. this.connectTimeout = undefined;
  104. }
  105. });
  106. }
  107. /**
  108. * Disconnects from all namespaces and closes transport.
  109. *
  110. * @private
  111. */
  112. _disconnect() {
  113. for (const socket of this.sockets.values()) {
  114. socket.disconnect();
  115. }
  116. this.sockets.clear();
  117. this.close();
  118. }
  119. /**
  120. * Removes a socket. Called by each `Socket`.
  121. *
  122. * @private
  123. */
  124. _remove(socket) {
  125. if (this.sockets.has(socket.id)) {
  126. const nsp = this.sockets.get(socket.id).nsp.name;
  127. this.sockets.delete(socket.id);
  128. this.nsps.delete(nsp);
  129. }
  130. else {
  131. debug("ignoring remove for %s", socket.id);
  132. }
  133. }
  134. /**
  135. * Closes the underlying connection.
  136. *
  137. * @private
  138. */
  139. close() {
  140. if ("open" === this.conn.readyState) {
  141. debug("forcing transport close");
  142. this.conn.close();
  143. this.onclose("forced server close");
  144. }
  145. }
  146. /**
  147. * Writes a packet to the transport.
  148. *
  149. * @param {Object} packet object
  150. * @param {Object} opts
  151. * @private
  152. */
  153. _packet(packet, opts = {}) {
  154. if (this.conn.readyState !== "open") {
  155. debug("ignoring packet write %j", packet);
  156. return;
  157. }
  158. const encodedPackets = opts.preEncoded
  159. ? packet // previous versions of the adapter incorrectly used socket.packet() instead of writeToEngine()
  160. : this.encoder.encode(packet);
  161. for (const encodedPacket of encodedPackets) {
  162. this.writeToEngine(encodedPacket, opts);
  163. }
  164. }
  165. writeToEngine(encodedPacket, opts) {
  166. if (opts.volatile && !this.conn.transport.writable) {
  167. debug("volatile packet is discarded since the transport is not currently writable");
  168. return;
  169. }
  170. this.conn.write(encodedPacket, opts);
  171. }
  172. /**
  173. * Called with incoming transport data.
  174. *
  175. * @private
  176. */
  177. ondata(data) {
  178. // try/catch is needed for protocol violations (GH-1880)
  179. try {
  180. this.decoder.add(data);
  181. }
  182. catch (e) {
  183. this.onerror(e);
  184. }
  185. }
  186. /**
  187. * Called when parser fully decodes a packet.
  188. *
  189. * @private
  190. */
  191. ondecoded(packet) {
  192. if (socket_io_parser_1.PacketType.CONNECT === packet.type) {
  193. if (this.conn.protocol === 3) {
  194. const parsed = url.parse(packet.nsp, true);
  195. this.connect(parsed.pathname, parsed.query);
  196. }
  197. else {
  198. this.connect(packet.nsp, packet.data);
  199. }
  200. }
  201. else {
  202. const socket = this.nsps.get(packet.nsp);
  203. if (socket) {
  204. process.nextTick(function () {
  205. socket._onpacket(packet);
  206. });
  207. }
  208. else {
  209. debug("no socket for namespace %s", packet.nsp);
  210. }
  211. }
  212. }
  213. /**
  214. * Handles an error.
  215. *
  216. * @param {Object} err object
  217. * @private
  218. */
  219. onerror(err) {
  220. for (const socket of this.sockets.values()) {
  221. socket._onerror(err);
  222. }
  223. this.conn.close();
  224. }
  225. /**
  226. * Called upon transport close.
  227. *
  228. * @param reason
  229. * @private
  230. */
  231. onclose(reason) {
  232. debug("client close with reason %s", reason);
  233. // ignore a potential subsequent `close` event
  234. this.destroy();
  235. // `nsps` and `sockets` are cleaned up seamlessly
  236. for (const socket of this.sockets.values()) {
  237. socket._onclose(reason);
  238. }
  239. this.sockets.clear();
  240. this.decoder.destroy(); // clean up decoder
  241. }
  242. /**
  243. * Cleans up event listeners.
  244. * @private
  245. */
  246. destroy() {
  247. this.conn.removeListener("data", this.ondata);
  248. this.conn.removeListener("error", this.onerror);
  249. this.conn.removeListener("close", this.onclose);
  250. // @ts-ignore
  251. this.decoder.removeListener("decoded", this.ondecoded);
  252. if (this.connectTimeout) {
  253. clearTimeout(this.connectTimeout);
  254. this.connectTimeout = undefined;
  255. }
  256. }
  257. }
  258. exports.Client = Client;