9 json = require('json');
37 * Packet type `connect`.
45 * Packet type `disconnect`.
50 exports.DISCONNECT = 1;
53 * Packet type `event`.
69 * Packet type `error`.
79 * @param {Object} packet
80 * @return {String} encoded
84 exports.encode = function(obj){
91 // if we have a namespace other than `/`
92 // we append it followed by a comma `,`
93 if (obj.nsp && '/' != obj.nsp) {
98 // immediately followed by the id
110 str += json.stringify(obj.data);
119 * @param {String} str
120 * @return {Object} packet
124 exports.decode = function(str){
129 p.type = Number(str.charAt(0));
130 if (null == exports.types[p.type]) return error();
132 // look up namespace (if any)
133 if ('/' == str.charAt(i + 1)) {
136 var c = str.charAt(i);
139 if (i + 1 == str.length) break;
146 var next = str.charAt(i + 1);
147 if ('' != next && Number(next) == next) {
150 var c = str.charAt(i);
151 if (null == c || Number(c) != c) {
155 p.id += str.charAt(i);
156 if (i + 1 == str.length) break;
161 if (str.charAt(++i)) {
163 p.data = json.parse(str.substr(i));
172 function error(data){