6 var EventEmitter = require('events').EventEmitter;
7 var parser = require('engine.io-parser');
8 var util = require('util');
9 var debug = require('debug')('engine:transport');
12 * Expose the constructor.
15 module.exports = Transport;
26 * Transport constructor.
28 * @param {http.IncomingMessage} request
32 function Transport (req) {
33 this.readyState = 'open';
34 this.discarded = false;
38 * Inherits from EventEmitter.
41 util.inherits(Transport, EventEmitter);
44 * Flags the transport as discarded.
49 Transport.prototype.discard = function () {
50 this.discarded = true;
54 * Called with an incoming HTTP request.
56 * @param {http.IncomingMessage} request
60 Transport.prototype.onRequest = function (req) {
61 debug('setting request');
66 * Closes the transport.
71 Transport.prototype.close = function (fn) {
72 if ('closed' === this.readyState || 'closing' === this.readyState) return;
74 this.readyState = 'closing';
75 this.doClose(fn || noop);
79 * Called with a transport error.
81 * @param {String} message error
82 * @param {Object} error description
86 Transport.prototype.onError = function (msg, desc) {
87 if (this.listeners('error').length) {
88 var err = new Error(msg);
89 err.type = 'TransportError';
90 err.description = desc;
91 this.emit('error', err);
93 debug('ignored transport error %s (%s)', msg, desc);
98 * Called with parsed out a packets from the data stream.
100 * @param {Object} packet
104 Transport.prototype.onPacket = function (packet) {
105 this.emit('packet', packet);
109 * Called with the encoded packet data.
111 * @param {String} data
115 Transport.prototype.onData = function (data) {
116 this.onPacket(parser.decodePacket(data));
120 * Called upon transport close.
125 Transport.prototype.onClose = function () {
126 this.readyState = 'closed';