ChainPadSrv.js : come back and clean up parseMessage just a little bit

pull/1/head
ansuz 9 years ago
parent 7c2d9e061f
commit db2fcda655

@ -24,16 +24,18 @@ var PING = 4;
var PONG = 5; var PONG = 5;
var parseMessage = function (msg) { var parseMessage = function (msg) {
var orig=msg, var res ={};
res ={};
// two or more? use a for // two or more? use a for
['pass','user','channelId','content'].forEach(function(attr){ ['pass','user','channelId','content'].forEach(function(attr){
var len=msg.substring(0,msg.indexOf(':')); var len=msg.slice(0,msg.indexOf(':')),
msg=msg.substring(len.length+1); // taking an offset lets us slice out the prop
var prop=msg.substring(0,Number(len)); // and saves us one string copy
msg = msg.substring(prop.length); o=len.length+1,
res[attr]=prop; prop=res[attr]=msg.slice(o,Number(len)+o);
// slice off the property and its descriptor
msg = msg.slice(prop.length+o);
}); });
// content is the only attribute that's not a string
res.content=JSON.parse(res.content); res.content=JSON.parse(res.content);
return res; return res;
}; };

Loading…
Cancel
Save