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.

websocket.js 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. const Transport = require("../transport");
  2. const debug = require("debug")("engine:ws");
  3. class WebSocket extends Transport {
  4. /**
  5. * WebSocket transport
  6. *
  7. * @param {http.IncomingMessage}
  8. * @api public
  9. */
  10. constructor(req) {
  11. super(req);
  12. this.socket = req.websocket;
  13. this.socket.on("message", this.onData.bind(this));
  14. this.socket.once("close", this.onClose.bind(this));
  15. this.socket.on("error", this.onError.bind(this));
  16. this.writable = true;
  17. this.perMessageDeflate = null;
  18. }
  19. /**
  20. * Transport name
  21. *
  22. * @api public
  23. */
  24. get name() {
  25. return "websocket";
  26. }
  27. /**
  28. * Advertise upgrade support.
  29. *
  30. * @api public
  31. */
  32. get handlesUpgrades() {
  33. return true;
  34. }
  35. /**
  36. * Advertise framing support.
  37. *
  38. * @api public
  39. */
  40. get supportsFraming() {
  41. return true;
  42. }
  43. /**
  44. * Processes the incoming data.
  45. *
  46. * @param {String} encoded packet
  47. * @api private
  48. */
  49. onData(data) {
  50. debug('received "%s"', data);
  51. super.onData(data);
  52. }
  53. /**
  54. * Writes a packet payload.
  55. *
  56. * @param {Array} packets
  57. * @api private
  58. */
  59. send(packets) {
  60. const packet = packets.shift();
  61. if (typeof packet === "undefined") {
  62. this.writable = true;
  63. this.emit("drain");
  64. return;
  65. }
  66. // always creates a new object since ws modifies it
  67. const opts = {};
  68. if (packet.options) {
  69. opts.compress = packet.options.compress;
  70. }
  71. const send = data => {
  72. if (this.perMessageDeflate) {
  73. const len =
  74. "string" === typeof data ? Buffer.byteLength(data) : data.length;
  75. if (len < this.perMessageDeflate.threshold) {
  76. opts.compress = false;
  77. }
  78. }
  79. debug('writing "%s"', data);
  80. this.writable = false;
  81. this.socket.send(data, opts, err => {
  82. if (err) return this.onError("write error", err.stack);
  83. this.send(packets);
  84. });
  85. };
  86. if (packet.options && typeof packet.options.wsPreEncoded === "string") {
  87. send(packet.options.wsPreEncoded);
  88. } else {
  89. this.parser.encodePacket(packet, this.supportsBinary, send);
  90. }
  91. }
  92. /**
  93. * Closes the transport.
  94. *
  95. * @api private
  96. */
  97. doClose(fn) {
  98. debug("closing");
  99. this.socket.close();
  100. fn && fn();
  101. }
  102. }
  103. module.exports = WebSocket;