Skip to content

Commit

Permalink
emit actual socket on debug events
Browse files Browse the repository at this point in the history
  • Loading branch information
Contra committed Mar 8, 2013
1 parent ba28371 commit dfb7979
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 69 deletions.
80 changes: 49 additions & 31 deletions examples/holla.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ var err = { type: 'error', data: 'parser error' }
*/

exports.encodePacket = function (packet) {
var encoded = packets[packet.type]
var encoded = packets[packet.type];

// data fragment is optional
if (undefined !== packet.data) {
Expand Down Expand Up @@ -314,62 +314,61 @@ exports.encodePayload = function (packets) {
/*
* Decodes data when a payload is maybe expected.
*
* @param {String} data
* @return {Array} packets
* @param {String} data, callback method
* @return {NaN}
* @api public
*/

exports.decodePayload = function (data) {
exports.decodePayload = function (data, callback) {
var packet;
if (data == '') {
// parser error - ignoring payload
return [err];
return callback(err, 0, 1);
}

var packets = []
, length = ''
, n, msg, packet
var length = ''
, n, msg;

for (var i = 0, l = data.length; i < l; i++) {
var chr = data.charAt(i)
var chr = data.charAt(i);

if (':' != chr) {
length += chr;
} else {
if ('' == length || (length != (n = Number(length)))) {
// parser error - ignoring payload
return [err];
return callback(err, 0, 1);
}

msg = data.substr(i + 1, n);

if (length != msg.length) {
// parser error - ignoring payload
return [err];
return callback(err, 0, 1);
}

if (msg.length) {
packet = exports.decodePacket(msg);

if (err.type == packet.type && err.data == packet.data) {
// parser error in individual packet - ignoring payload
return [err];
return callback(err, 0, 1);
}

packets.push(packet);
callback(packet, i + n, l);
}

// advance cursor
i += n;
length = ''
length = '';
}
}

if (length != '') {
// parser error - ignoring payload
return [err];
return callback(err, 0, 1);
}

return packets;
};

});
Expand Down Expand Up @@ -1717,26 +1716,33 @@ Polling.prototype.poll = function(){
*/

Polling.prototype.onData = function(data){
var self = this;
debug('polling got data %s', data);
// decode payload
var packets = parser.decodePayload(data);

for (var i = 0, l = packets.length; i < l; i++) {
// if its the first message we consider the trnasport open
if ('opening' == this.readyState) {
this.onOpen();
}
parser.decodePayload(data, function(packet, index, total) {self.onDataCallback(packet, index, total)});
};

// if its a close packet, we close the ongoing requests
if ('close' == packets[i].type) {
this.onClose();
return;
}
/**
* Callback function for payloads
*
* @api private
*/

Polling.prototype.onDataCallback = function(packet, index, total){
// if its the first message we consider the transport open
if ('opening' == this.readyState) {
this.onOpen();
}

// otherwise bypass onData and handle the message
this.onPacket(packets[i]);
// if its a close packet, we close the ongoing requests
if ('close' == packet.type) {
this.onClose();
return;
}

// otherwise bypass onData and handle the message
this.onPacket(packet);

// if we got data we're not polling
this.polling = false;
this.emit('pollComplete');
Expand Down Expand Up @@ -2293,7 +2299,11 @@ JSONPPolling.prototype.doWrite = function (data, fn) {

function initIframe () {
if (self.iframe) {
self.form.removeChild(self.iframe);
try {
self.form.removeChild(self.iframe);
} catch (e) {
self.onError('jsonp polling iframe removal error', e);
}
}

try {
Expand Down Expand Up @@ -3205,6 +3215,14 @@ Emitter.prototype.off =
Emitter.prototype.removeListener =
Emitter.prototype.removeAllListeners = function(event, fn){
this._callbacks = this._callbacks || {};

// all
if (0 == arguments.length) {
this._callbacks = {};
return this;
}

// specific event
var callbacks = this._callbacks[event];
if (!callbacks) return this;

Expand Down
80 changes: 49 additions & 31 deletions holla.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ var err = { type: 'error', data: 'parser error' }
*/

exports.encodePacket = function (packet) {
var encoded = packets[packet.type]
var encoded = packets[packet.type];

// data fragment is optional
if (undefined !== packet.data) {
Expand Down Expand Up @@ -314,62 +314,61 @@ exports.encodePayload = function (packets) {
/*
* Decodes data when a payload is maybe expected.
*
* @param {String} data
* @return {Array} packets
* @param {String} data, callback method
* @return {NaN}
* @api public
*/

exports.decodePayload = function (data) {
exports.decodePayload = function (data, callback) {
var packet;
if (data == '') {
// parser error - ignoring payload
return [err];
return callback(err, 0, 1);
}

var packets = []
, length = ''
, n, msg, packet
var length = ''
, n, msg;

for (var i = 0, l = data.length; i < l; i++) {
var chr = data.charAt(i)
var chr = data.charAt(i);

if (':' != chr) {
length += chr;
} else {
if ('' == length || (length != (n = Number(length)))) {
// parser error - ignoring payload
return [err];
return callback(err, 0, 1);
}

msg = data.substr(i + 1, n);

if (length != msg.length) {
// parser error - ignoring payload
return [err];
return callback(err, 0, 1);
}

if (msg.length) {
packet = exports.decodePacket(msg);

if (err.type == packet.type && err.data == packet.data) {
// parser error in individual packet - ignoring payload
return [err];
return callback(err, 0, 1);
}

packets.push(packet);
callback(packet, i + n, l);
}

// advance cursor
i += n;
length = ''
length = '';
}
}

if (length != '') {
// parser error - ignoring payload
return [err];
return callback(err, 0, 1);
}

return packets;
};

});
Expand Down Expand Up @@ -1717,26 +1716,33 @@ Polling.prototype.poll = function(){
*/

Polling.prototype.onData = function(data){
var self = this;
debug('polling got data %s', data);
// decode payload
var packets = parser.decodePayload(data);

for (var i = 0, l = packets.length; i < l; i++) {
// if its the first message we consider the trnasport open
if ('opening' == this.readyState) {
this.onOpen();
}
parser.decodePayload(data, function(packet, index, total) {self.onDataCallback(packet, index, total)});
};

// if its a close packet, we close the ongoing requests
if ('close' == packets[i].type) {
this.onClose();
return;
}
/**
* Callback function for payloads
*
* @api private
*/

Polling.prototype.onDataCallback = function(packet, index, total){
// if its the first message we consider the transport open
if ('opening' == this.readyState) {
this.onOpen();
}

// otherwise bypass onData and handle the message
this.onPacket(packets[i]);
// if its a close packet, we close the ongoing requests
if ('close' == packet.type) {
this.onClose();
return;
}

// otherwise bypass onData and handle the message
this.onPacket(packet);

// if we got data we're not polling
this.polling = false;
this.emit('pollComplete');
Expand Down Expand Up @@ -2293,7 +2299,11 @@ JSONPPolling.prototype.doWrite = function (data, fn) {

function initIframe () {
if (self.iframe) {
self.form.removeChild(self.iframe);
try {
self.form.removeChild(self.iframe);
} catch (e) {
self.onError('jsonp polling iframe removal error', e);
}
}

try {
Expand Down Expand Up @@ -3205,6 +3215,14 @@ Emitter.prototype.off =
Emitter.prototype.removeListener =
Emitter.prototype.removeAllListeners = function(event, fn){
this._callbacks = this._callbacks || {};

// all
if (0 == arguments.length) {
this._callbacks = {};
return this;
}

// specific event
var callbacks = this._callbacks[event];
if (!callbacks) return this;

Expand Down
4 changes: 2 additions & 2 deletions holla.min.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions lib/Server.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ module.exports =

req =
name: socket.identity
socket: socket.identity
socket: socket
to: msg.to

@emit "offer", req
Expand All @@ -149,7 +149,7 @@ module.exports =

req =
name: socket.identity
socket: socket.identity
socket: socket
to: msg.to

@emit "hangup", req
Expand All @@ -165,7 +165,7 @@ module.exports =

req =
name: socket.identity
socket: socket.identity
socket: socket
to: msg.to
args:
accepted: msg.args.accepted
Expand Down Expand Up @@ -202,7 +202,7 @@ module.exports =

req =
name: socket.identity
socket: socket.identity
socket: socket
to: msg.to
message: msg.args.message

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "holla",
"description": "An abstraction over P2P video/voice/data connections using WebRTC",
"version": "0.6.1",
"version": "0.6.2",
"homepage": "http://github.com/wearefractal/holla",
"repository": "git://github.com/wearefractal/holla.git",
"author": "Fractal <[email protected]> (http://wearefractal.com/)",
Expand Down

0 comments on commit dfb7979

Please sign in to comment.