diff options
author | lookshe <github@lookshe.org> | 2015-03-14 20:45:20 +0100 |
---|---|---|
committer | lookshe <github@lookshe.org> | 2015-03-14 20:45:20 +0100 |
commit | b60df56157ee1fd0bd4938799bac05a62fda91a1 (patch) | |
tree | 2bc906c45ff9ec940e07f9676f5ed34ddd4ae022 /signaling-server/node_modules/socket.io/node_modules/socket.io-client/components/learnboost-socket.io-protocol |
initial commit from working version
Diffstat (limited to 'signaling-server/node_modules/socket.io/node_modules/socket.io-client/components/learnboost-socket.io-protocol')
2 files changed, 189 insertions, 0 deletions
diff --git a/signaling-server/node_modules/socket.io/node_modules/socket.io-client/components/learnboost-socket.io-protocol/component.json b/signaling-server/node_modules/socket.io/node_modules/socket.io-client/components/learnboost-socket.io-protocol/component.json new file mode 100644 index 0000000..82ee9f1 --- /dev/null +++ b/signaling-server/node_modules/socket.io/node_modules/socket.io-client/components/learnboost-socket.io-protocol/component.json @@ -0,0 +1,12 @@ +{ + "name": "socket.io-parser", + "version": "1.0.1", + "description": "socket.io protocol parser", + "dependencies": { + "component/json": "*" + }, + "scripts": [ + "index.js" + ], + "repo": "https://raw.github.com/learnboost/socket.io-protocol" +}
\ No newline at end of file diff --git a/signaling-server/node_modules/socket.io/node_modules/socket.io-client/components/learnboost-socket.io-protocol/index.js b/signaling-server/node_modules/socket.io/node_modules/socket.io-client/components/learnboost-socket.io-protocol/index.js new file mode 100644 index 0000000..167e8fb --- /dev/null +++ b/signaling-server/node_modules/socket.io/node_modules/socket.io-client/components/learnboost-socket.io-protocol/index.js @@ -0,0 +1,177 @@ + +/** + * Module dependencies. + */ + +var json; + +try { + json = require('json'); +} catch(e){ + json = JSON; +} + +/** + * Protocol version. + * + * @api public + */ + +exports.protocol = 1; + +/** + * Packet types. + * + * @api public + */ + +exports.types = [ + 'CONNECT', + 'DISCONNECT', + 'EVENT', + 'ACK', + 'ERROR' +]; + +/** + * Packet type `connect`. + * + * @api public + */ + +exports.CONNECT = 0; + +/** + * Packet type `disconnect`. + * + * @api public + */ + +exports.DISCONNECT = 1; + +/** + * Packet type `event`. + * + * @api public + */ + +exports.EVENT = 2; + +/** + * Packet type `ack`. + * + * @api public + */ + +exports.ACK = 3; + +/** + * Packet type `error`. + * + * @api public + */ + +exports.ERROR = 4; + +/** + * Encode. + * + * @param {Object} packet + * @return {String} encoded + * @api public + */ + +exports.encode = function(obj){ + var str = ''; + var nsp = false; + + // first is type + str += obj.type; + + // if we have a namespace other than `/` + // we append it followed by a comma `,` + if (obj.nsp && '/' != obj.nsp) { + nsp = true; + str += obj.nsp; + } + + // immediately followed by the id + if (obj.id) { + if (nsp) { + str += ','; + nsp = false; + } + str += obj.id; + } + + // json data + if (obj.data) { + if (nsp) str += ','; + str += json.stringify(obj.data); + } + + return str; +}; + +/** + * Decode. + * + * @param {String} str + * @return {Object} packet + * @api public + */ + +exports.decode = function(str){ + var p = {}; + var i = 0; + + // look up type + p.type = Number(str.charAt(0)); + if (null == exports.types[p.type]) return error(); + + // look up namespace (if any) + if ('/' == str.charAt(i + 1)) { + p.nsp = ''; + while (++i) { + var c = str.charAt(i); + if (',' == c) break; + p.nsp += c; + if (i + 1 == str.length) break; + } + } else { + p.nsp = '/'; + } + + // look up id + var next = str.charAt(i + 1); + if ('' != next && Number(next) == next) { + p.id = ''; + while (++i) { + var c = str.charAt(i); + if (null == c || Number(c) != c) { + --i; + break; + } + p.id += str.charAt(i); + if (i + 1 == str.length) break; + } + } + + // look up json data + if (str.charAt(++i)) { + try { + p.data = json.parse(str.substr(i)); + } catch(e){ + return error(); + } + } + + return p; +}; + +function error(data){ + return { + type: exports.ERROR, + data: 'parser error' + }; +} |