comunicationLeap/node_modules/engine.io/lib/transport.js
node_modules/engine.io/lib/transport.jsRaw
1
2/**
3 * Module dependencies.
4 */
5
6var EventEmitter = require('events').EventEmitter
7 , parser = require('engine.io-parser')
8 , debug = require('debug')('engine:transport');
9
10/**
11 * Expose the constructor.
12 */
13
14module.exports = Transport;
15
16/**
17 * Noop function.
18 *
19 * @api private
20 */
21
22function noop () {};
23
24/**
25 * Transport constructor.
26 *
27 * @param {http.ServerRequest} request
28 * @api public
29 */
30
31function Transport (req) {
32 this.readyState = 'opening';
33};
34
35/**
36 * Inherits from EventEmitter.
37 */
38
39Transport.prototype.__proto__ = EventEmitter.prototype;
40
41/**
42 * Called with an incoming HTTP request.
43 *
44 * @param {http.ServerRequest} request
45 * @api private
46 */
47
48Transport.prototype.onRequest = function (req) {
49 debug('setting request');
50 this.req = req;
51};
52
53/**
54 * Closes the transport.
55 *
56 * @api private
57 */
58
59Transport.prototype.close = function (fn) {
60 this.readyState = 'closing';
61 this.doClose(fn || noop);
62};
63
64/**
65 * Called with a transport error.
66 *
67 * @param {String} message error
68 * @param {Object} error description
69 * @api private
70 */
71
72Transport.prototype.onError = function (msg, desc) {
73 if (this.listeners('error').length) {
74 var err = new Error(msg);
75 err.type = 'TransportError';
76 err.description = desc;
77 this.emit('error', err);
78 } else {
79 debug('ignored transport error %s (%s)', msg, desc);
80 }
81};
82
83/**
84 * Called with parsed out a packets from the data stream.
85 *
86 * @param {Object} packet
87 * @api private
88 */
89
90Transport.prototype.onPacket = function (packet) {
91 this.emit('packet', packet);
92};
93
94/**
95 * Called with the encoded packet data.
96 *
97 * @param {String} data
98 * @api private
99 */
100
101Transport.prototype.onData = function (data) {
102 this.onPacket(parser.decodePacket(data));
103};
104
105/**
106 * Called upon transport close.
107 *
108 * @api private
109 */
110
111Transport.prototype.onClose = function () {
112 this.readyState = 'closed';
113 this.emit('close');
114};