Permalink
Browse files

Added _onData to handle data decoding

_onMessage added as callback to the decoder
Added proper message type handling
Implemented _onData in the transports
  • Loading branch information...
1 parent be05788 commit c97264a547d00043bc4d77c5921b1d8173db114a @rauchg rauchg committed Nov 13, 2010
View
@@ -4,6 +4,7 @@ var urlparse = require('url').parse
, Decoder = require('./data').Decoder
, encode = require('./data').encode
, encodeMessage = require('./data').encodeMessage
+ , decodeMessage = require('./data').decodeMessage
, options = require('./utils').options
, merge = require('./utils').merge;
@@ -21,6 +22,7 @@ var Client = module.exports = function(listener, req, res, options, head){
this.connected = false;
this.upgradeHead = head;
this.decoder = new Decoder();
+ this.decoder.on('data', this._onMessage.bind(this));
this._onConnect(req, res);
};
@@ -41,20 +43,25 @@ Client.prototype.broadcast = function(message){
return this;
};
-Client.prototype._onMessage = function(data){
- var messages = decode(data);
- if (messages === false) return this.listener.options.log('Bad message received from client ' + this.sessionId);
- for (var i = 0, l = messages.length, frame; i < l; i++){
- frame = messages[i].substr(0, 3);
- switch (frame){
- case '~h~':
- return this._onHeartbeat(messages[i].substr(3));
- case '~j~':
- messages[i] = JSON.parse(messages[i].substr(3));
- break;
- }
- this.emit('message', messages[i]);
- this.listener._onClientMessage(messages[i], this);
+Client.prototype._onData = function(data){
+ this.decoder.add(data);
+}
+
+Client.prototype._onMessage = function(type, data){
+ switch (type){
+ case '0':
+ this._onDisconnect();
+ break;
+
+ case '1':
+ var msg = decodeMessage(data);
+ // handle json decoding
+ if ('j' in msg[1]) msg[0] = JSON.parse(msg[0]);
+ this.emit('message', msg[0], msg[1]);
+ break;
+
+ case '2':
+ this._onHeartbeat(data);
}
};
@@ -30,7 +30,7 @@ HTMLFile.prototype._onConnect = function(req, res){
req.addListener('end', function(){
try {
var msg = qs.parse(body);
- self._onMessage(msg.data);
+ self._onData(msg.data);
} catch(e){}
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('ok');
@@ -43,4 +43,4 @@ HTMLFile.prototype._onConnect = function(req, res){
HTMLFile.prototype._write = function(message){
if (this._open)
this.response.write('<script>parent.s._('+ JSON.stringify(message) +', document);</script>'); //json for escaping
-};
+};
@@ -78,7 +78,7 @@ WebSocket.prototype._handle = function(data){
this._onClose();
return false;
}
- this._onMessage(chunk.slice(1));
+ this._onData(chunk.slice(1));
}
this.data = chunks[chunks.length - 1];
};
@@ -133,4 +133,4 @@ WebSocket.prototype._write = function(message){
}
};
-WebSocket.httpUpgrade = true;
+WebSocket.httpUpgrade = true;
@@ -44,7 +44,7 @@ Multipart.prototype._onConnect = function(req, res){
req.addListener('end', function(){
try {
var msg = qs.parse(body);
- self._onMessage(msg.data);
+ self._onData(msg.data);
} catch(e){}
res.writeHead(200, headers);
res.write('ok');
@@ -61,4 +61,4 @@ Multipart.prototype._write = function(message){
this.response.write(message + "\n");
this.response.write("--socketio\n");
}
-};
+};
@@ -47,7 +47,7 @@ Polling.prototype._onConnect = function(req, res){
try {
// optimization: just strip first 5 characters here?
var msg = qs.parse(body);
- self._onMessage(msg.data);
+ self._onData(msg.data);
} catch(e){}
res.writeHead(200, headers);
res.write('ok');
@@ -75,4 +75,4 @@ Polling.prototype._write = function(message){
this.response.end();
this._onClose();
}
-};
+};

0 comments on commit c97264a

Please sign in to comment.