123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- var EventEmitter = require('events').EventEmitter;
- var parser = require('engine.io-parser');
- var util = require('util');
- var debug = require('debug')('engine:transport');
- module.exports = Transport;
- function noop () {}
- function Transport (req) {
- this.readyState = 'open';
- this.discarded = false;
- }
- util.inherits(Transport, EventEmitter);
- Transport.prototype.discard = function () {
- this.discarded = true;
- };
- Transport.prototype.onRequest = function (req) {
- debug('setting request');
- this.req = req;
- };
- Transport.prototype.close = function (fn) {
- if ('closed' === this.readyState || 'closing' === this.readyState) return;
- this.readyState = 'closing';
- this.doClose(fn || noop);
- };
- Transport.prototype.onError = function (msg, desc) {
- if (this.listeners('error').length) {
- var err = new Error(msg);
- err.type = 'TransportError';
- err.description = desc;
- this.emit('error', err);
- } else {
- debug('ignored transport error %s (%s)', msg, desc);
- }
- };
- Transport.prototype.onPacket = function (packet) {
- this.emit('packet', packet);
- };
- Transport.prototype.onData = function (data) {
- this.onPacket(parser.decodePacket(data));
- };
- Transport.prototype.onClose = function () {
- this.readyState = 'closed';
- this.emit('close');
- };
|