socketioMEANseed/server/node_modules/engine.io-client/lib/transport.js
server/node_modules/engine.io-client/lib/transport.jsRaw
1/**
2 * Module dependencies.
3 */
4
5var parser = require('engine.io-parser');
6var Emitter = require('component-emitter');
7
8/**
9 * Module exports.
10 */
11
12module.exports = Transport;
13
14/**
15 * Transport abstract constructor.
16 *
17 * @param {Object} options.
18 * @api private
19 */
20
21function Transport (opts) {
22 this.path = opts.path;
23 this.hostname = opts.hostname;
24 this.port = opts.port;
25 this.secure = opts.secure;
26 this.query = opts.query;
27 this.timestampParam = opts.timestampParam;
28 this.timestampRequests = opts.timestampRequests;
29 this.readyState = '';
30 this.agent = opts.agent || false;
31 this.socket = opts.socket;
32 this.enablesXDR = opts.enablesXDR;
33
34 // SSL options for Node.js client
35 this.pfx = opts.pfx;
36 this.key = opts.key;
37 this.passphrase = opts.passphrase;
38 this.cert = opts.cert;
39 this.ca = opts.ca;
40 this.ciphers = opts.ciphers;
41 this.rejectUnauthorized = opts.rejectUnauthorized;
42 this.forceNode = opts.forceNode;
43
44 // other options for Node.js client
45 this.extraHeaders = opts.extraHeaders;
46 this.localAddress = opts.localAddress;
47}
48
49/**
50 * Mix in `Emitter`.
51 */
52
53Emitter(Transport.prototype);
54
55/**
56 * Emits an error.
57 *
58 * @param {String} str
59 * @return {Transport} for chaining
60 * @api public
61 */
62
63Transport.prototype.onError = function (msg, desc) {
64 var err = new Error(msg);
65 err.type = 'TransportError';
66 err.description = desc;
67 this.emit('error', err);
68 return this;
69};
70
71/**
72 * Opens the transport.
73 *
74 * @api public
75 */
76
77Transport.prototype.open = function () {
78 if ('closed' === this.readyState || '' === this.readyState) {
79 this.readyState = 'opening';
80 this.doOpen();
81 }
82
83 return this;
84};
85
86/**
87 * Closes the transport.
88 *
89 * @api private
90 */
91
92Transport.prototype.close = function () {
93 if ('opening' === this.readyState || 'open' === this.readyState) {
94 this.doClose();
95 this.onClose();
96 }
97
98 return this;
99};
100
101/**
102 * Sends multiple packets.
103 *
104 * @param {Array} packets
105 * @api private
106 */
107
108Transport.prototype.send = function (packets) {
109 if ('open' === this.readyState) {
110 this.write(packets);
111 } else {
112 throw new Error('Transport not open');
113 }
114};
115
116/**
117 * Called upon open
118 *
119 * @api private
120 */
121
122Transport.prototype.onOpen = function () {
123 this.readyState = 'open';
124 this.writable = true;
125 this.emit('open');
126};
127
128/**
129 * Called with data.
130 *
131 * @param {String} data
132 * @api private
133 */
134
135Transport.prototype.onData = function (data) {
136 var packet = parser.decodePacket(data, this.socket.binaryType);
137 this.onPacket(packet);
138};
139
140/**
141 * Called with a decoded packet.
142 */
143
144Transport.prototype.onPacket = function (packet) {
145 this.emit('packet', packet);
146};
147
148/**
149 * Called upon close.
150 *
151 * @api private
152 */
153
154Transport.prototype.onClose = function () {
155 this.readyState = 'closed';
156 this.emit('close');
157};