6 var util = require('./util')
7 , parser = require('./parser')
8 , Emitter = require('./emitter');
14 module.exports = Transport;
17 * Transport abstract constructor.
19 * @param {Object} options.
23 function Transport (opts) {
24 this.path = opts.path;
25 this.host = opts.host;
26 this.port = opts.port;
27 this.secure = opts.secure;
28 this.query = opts.query;
29 this.timestampParam = opts.timestampParam;
30 this.timestampRequests = opts.timestampRequests;
38 Emitter(Transport.prototype);
44 * @return {Transport} for chaining
48 Transport.prototype.onError = function (msg, desc) {
49 var err = new Error(msg);
50 err.type = 'TransportError';
51 err.description = desc;
52 this.emit('error', err);
57 * Opens the transport.
62 Transport.prototype.open = function () {
63 if ('closed' == this.readyState || '' == this.readyState) {
64 this.readyState = 'opening';
72 * Closes the transport.
77 Transport.prototype.close = function () {
78 if ('opening' == this.readyState || 'open' == this.readyState) {
87 * Sends multiple packets.
89 * @param {Array} packets
93 Transport.prototype.send = function(packets){
94 if ('open' == this.readyState) {
97 throw new Error('Transport not open');
107 Transport.prototype.onOpen = function () {
108 this.readyState = 'open';
109 this.writable = true;
116 * @param {String} data
120 Transport.prototype.onData = function (data) {
121 this.onPacket(parser.decodePacket(data));
125 * Called with a decoded packet.
128 Transport.prototype.onPacket = function (packet) {
129 this.emit('packet', packet);
138 Transport.prototype.onClose = function () {
139 this.readyState = 'closed';