5 var parser = require('engine.io-parser');
6 var Emitter = require('component-emitter');
12 module.exports = Transport;
15 * Transport abstract constructor.
17 * @param {Object} options.
21 function 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;
30 this.agent = opts.agent || false;
31 this.socket = opts.socket;
32 this.enablesXDR = opts.enablesXDR;
33 this.withCredentials = opts.withCredentials;
35 // SSL options for Node.js client
38 this.passphrase = opts.passphrase;
39 this.cert = opts.cert;
41 this.ciphers = opts.ciphers;
42 this.rejectUnauthorized = opts.rejectUnauthorized;
43 this.forceNode = opts.forceNode;
45 // results of ReactNative environment detection
46 this.isReactNative = opts.isReactNative;
48 // other options for Node.js client
49 this.extraHeaders = opts.extraHeaders;
50 this.localAddress = opts.localAddress;
57 Emitter(Transport.prototype);
63 * @return {Transport} for chaining
67 Transport.prototype.onError = function (msg, desc) {
68 var err = new Error(msg);
69 err.type = 'TransportError';
70 err.description = desc;
71 this.emit('error', err);
76 * Opens the transport.
81 Transport.prototype.open = function () {
82 if ('closed' === this.readyState || '' === this.readyState) {
83 this.readyState = 'opening';
91 * Closes the transport.
96 Transport.prototype.close = function () {
97 if ('opening' === this.readyState || 'open' === this.readyState) {
106 * Sends multiple packets.
108 * @param {Array} packets
112 Transport.prototype.send = function (packets) {
113 if ('open' === this.readyState) {
116 throw new Error('Transport not open');
126 Transport.prototype.onOpen = function () {
127 this.readyState = 'open';
128 this.writable = true;
135 * @param {String} data
139 Transport.prototype.onData = function (data) {
140 var packet = parser.decodePacket(data, this.socket.binaryType);
141 this.onPacket(packet);
145 * Called with a decoded packet.
148 Transport.prototype.onPacket = function (packet) {
149 this.emit('packet', packet);
158 Transport.prototype.onClose = function () {
159 this.readyState = 'closed';