add method
dynamic
add(
- dynamic obj
Decodes an ecoded packet string into packet JSON.
@param {String} obj - encoded packet @return {Object} packet @api public
Implementation
add(obj) {
var packet;
if (obj is String) {
packet = decodeString(obj);
if (BINARY_EVENT == packet['type'] || BINARY_ACK == packet['type']) {
// binary packet's json
this.reconstructor = new BinaryReconstructor(packet);
// no attachments, labeled binary but no binary data to follow
if (this.reconstructor.reconPack['attachments'] == 0) {
this.emit('decoded', packet);
}
} else {
// non-binary full packet
this.emit('decoded', packet);
}
} else if (isBinary(obj) || obj is Map && obj['base64'] != null) {
// raw binary data
if (this.reconstructor == null) {
throw new UnsupportedError(
'got binary data when not reconstructing a packet');
} else {
packet = this.reconstructor.takeBinaryData(obj);
if (packet != null) {
// received final buffer
this.reconstructor = null;
this.emit('decoded', packet);
}
}
} else {
throw new UnsupportedError('Unknown type: ' + obj);
}
}