aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorAlan K <github@ack.modeswitch.org>2014-03-17 19:04:51 -0400
committerAlan K <github@ack.modeswitch.org>2014-03-17 19:04:51 -0400
commitc3a8ac0a5f1df80dbc3e3a3f42eb22b4f8959e50 (patch)
tree1d0b117bf5435aee30df0c95feadea1deee1ccc2 /tests
parent7e22558b8a8c108ef3804f05311607724d9f3eab (diff)
Fixed accidental submodule
Diffstat (limited to 'tests')
m---------tests/sockets/p2p0
-rw-r--r--tests/sockets/p2p/.gitignore2
-rw-r--r--tests/sockets/p2p/README.md98
l---------tests/sockets/p2p/bin/uglifyjs1
-rw-r--r--tests/sockets/p2p/broker/p2p-broker.js231
-rw-r--r--tests/sockets/p2p/client/p2p-client.js4485
-rw-r--r--tests/sockets/p2p/examples/data-demo.html16
-rw-r--r--tests/sockets/p2p/examples/data-demo.js103
-rw-r--r--tests/sockets/p2p/examples/list.html112
l---------tests/sockets/p2p/forever1
-rw-r--r--tests/sockets/p2p/package.json22
11 files changed, 5071 insertions, 0 deletions
diff --git a/tests/sockets/p2p b/tests/sockets/p2p
deleted file mode 160000
-Subproject f750484c56c49fb0383d488a9751c466590004b
diff --git a/tests/sockets/p2p/.gitignore b/tests/sockets/p2p/.gitignore
new file mode 100644
index 00000000..065a4ac0
--- /dev/null
+++ b/tests/sockets/p2p/.gitignore
@@ -0,0 +1,2 @@
+node_modules
+ssl
diff --git a/tests/sockets/p2p/README.md b/tests/sockets/p2p/README.md
new file mode 100644
index 00000000..52d383dd
--- /dev/null
+++ b/tests/sockets/p2p/README.md
@@ -0,0 +1,98 @@
+# WebRTC peer-to-peer
+
+This is a browser JS library that makes it easy to manage RTC peer connections, streams and data channels.
+It's currently used in [emscripten](http://github.com/kripken/emscripten) to provide data transport for the posix sockets implementation.
+
+## Requirements
+
+You will need either Firefox [Nightly](http://nightly.mozilla.org/), or Chrome [Canary](https://www.google.com/intl/en/chrome/browser/canary.html).
+You can also use Chrome [Dev Channel](http://www.chromium.org/getting-involved/dev-channel).
+
+## What it does
+
+* Firefox (nightly) and Chrome (dev/canary) supported
+* Binary transport using arraybuffers (Firefox only!)
+* Multiple connections
+* Broker service (on nodejitsu), or run your own
+* Connection timeouts
+
+## What it doesn't do (yet!)
+
+* Interoperability between Firefox and Chrome
+* Peer brokering for establishing new connections through existing peer-to-peer
+
+## Quick start
+
+Setting up a peer is easy. The code below will create a new peer and listen for incoming connections.
+The `onconnection` handler is called each time a new connection is ready.
+
+````javascript
+// Create a new Peer
+var peer = new Peer(
+ 'http://webrtcb.jit.su:80', // You can use this broker if you don't want to set one up
+ {
+ binaryType: 'arraybuffer',
+ video: false,
+ audio: false
+ }
+);
+
+// Listen for incoming connections
+peer.listen();
+
+var connections = {};
+
+// Handle new connections
+peer.onconnection = function(connection) {
+ // Store connections here so we can use them later
+ connections[connection.id] = connection; // Each connection has a unique ID
+
+ connection.ondisconnect = function(reason) {
+ delete connections[connection.id];
+ };
+
+ connection.onerror = function(error) {
+ console.error(error);
+ };
+
+ // Handle messages from this channel
+ // The label will be 'reliable' or 'unreliable', depending on how it was received
+ connection.onmessage = function(label, message) {
+ console.log(label, message);
+ };
+
+ // Sends a message to the other peer using the reliable data channel
+ connection.send('reliable', 'hi!');
+
+ // The connection exposes the underlying media streams
+ // You can attach them to DOM elements to get video/audio, if available
+ console.log(connection.streams.local, connection.streams.remote);
+
+ // Closes the connection
+ // This will cause `ondisconnect` to fire
+ connection.close();
+};
+
+// Print our route when it's available
+peer.onroute = function(route) {
+ // This is our routing address from the broker
+ // It's used by peers who wish to connect with us
+ console.log('route:', route);
+};
+
+peer.onerror = function(error) {
+ console.log(error);
+};
+````
+
+Another peer can connect easily to the one we made above by calling `connect()` on its routing address.
+
+````javascript
+peer.connect(route);
+````
+
+## Demo
+
+There are some files in the `demo` directory that offer an example.
+You can load it [here](http://js-platform.github.com/p2p/examples/data-demo.html) and open the `connect` URL in another window.
+For this example, the `route` is added to the URL query string so that the other peer can parse it and connect when the page loads, so all you need to share is the URL.
diff --git a/tests/sockets/p2p/bin/uglifyjs b/tests/sockets/p2p/bin/uglifyjs
new file mode 120000
index 00000000..94aae288
--- /dev/null
+++ b/tests/sockets/p2p/bin/uglifyjs
@@ -0,0 +1 @@
+../node_modules/.bin/uglifyjs \ No newline at end of file
diff --git a/tests/sockets/p2p/broker/p2p-broker.js b/tests/sockets/p2p/broker/p2p-broker.js
new file mode 100644
index 00000000..028eb25b
--- /dev/null
+++ b/tests/sockets/p2p/broker/p2p-broker.js
@@ -0,0 +1,231 @@
+var crypto = require('crypto');
+var fs = require('fs');
+var https = require('https');
+
+var SSL_KEY = 'ssl/ssl.key';
+var SSL_CERT = 'ssl/ssl-unified.crt';
+var PORT = 8080;
+
+var sslSupported = false;
+if(fs.existsSync(SSL_KEY) && fs.existsSync(SSL_CERT) && fs.statSync(SSL_KEY).isFile() && fs.statSync(SSL_CERT).isFile()) {
+ sslSupported = true;
+}
+
+function handler(req, res) {
+ res.writeHead(200);
+ res.end("p2p");
+};
+
+var app, port;
+if(sslSupported) {
+ var sslopts = {
+ key: fs.readFileSync(SSL_KEY),
+ cert: fs.readFileSync(SSL_CERT)
+ };
+ sslopts.agent = new https.Agent(sslopts);
+ app = require('https').createServer(sslopts, handler);
+ port = 8081;
+ console.info('ssl mode enabled');
+} else {
+ app = require('http').createServer(handler);
+ port = 8080;
+ console.info('ssl mode disabled');
+}
+console.info('listening on port', port);
+
+var io = require('socket.io').listen(app, {
+ 'log level': 2
+});
+
+app.listen(port);
+
+var jsMime = {
+ type: 'application/javascript',
+ encoding: 'utf8',
+ gzip: true
+};
+
+io.static.add('/p2p-client.js', {
+ mime: jsMime,
+ file: 'client/p2p-client.js'
+});
+
+/*
+io.static.add('/p2p-client.min.js', {
+ mime: jsMime,
+ file: 'client/p2p-client.min.js'
+});
+*/
+
+function mkguid() {
+ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
+ var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
+ return v.toString(16);
+ }).toUpperCase();
+};
+
+var peers = {};
+var hosts = {};
+
+var E = {
+ OK: 'ok'
+ , NOROUTE: 'no such route'
+ , ISNOTHOST: 'peer is not a host'
+};
+
+function Peer(socket) {
+ this.socket = socket;
+ this.host = null;
+};
+
+function Host(options) {
+ this.route = options['route'];
+ this.url = options['url'];
+ this.listed = (undefined !== options['listed']) ? options['listed'] : false;
+ this.metadata = options['metadata'] || {};
+ this.ctime = Date.now();
+ this.mtime = Date.now();
+};
+Host.prototype.update = function update(options) {
+ this.url = options['url'];
+ this.listed = (undefined !== options['listed']) ? options['listed'] : false;
+ this.metadata = options['metadata'] || {};
+ this.mtime = Date.now();
+};
+
+io.of('/peer').on('connection', function(socket) {
+ var route = crypto.createHash('md5').update(socket['id']).digest('hex');
+ socket.emit('route', route);
+
+ socket.on('disconnect', function() {
+ if(hosts[route]) {
+ var host = hosts[route];
+ changeList('remove', host);
+ }
+ delete hosts[route];
+ delete peers[route];
+ });
+
+ socket.on('send', function(message, callback) {
+ var to = message['to'];
+
+ if(!peers.hasOwnProperty(to)) {
+ callback({'error': E.NOROUTE});
+ return;
+ }
+
+ var from = route;
+ var data = message['data'];
+ peers[to].emit('receive', {
+ 'from': from,
+ 'data': data
+ });
+ });
+
+ socket.on('listen', function(options, callback) {
+ options['route'] = route;
+ if(hosts.hasOwnProperty(route)) {
+ hosts[route].update(options);
+ changeList('update', hosts[route]);
+ } else {
+ hosts[route] = new Host(options);
+ changeList('append', hosts[route]);
+ }
+
+ callback();
+ });
+
+ socket.on('ignore', function(message, callback) {
+ if(!hosts.hasOwnProperty(route)) {
+ callback({'error': E.ISNOTHOST});
+ return;
+ }
+
+ var host = hosts[route];
+ delete hosts[route];
+
+ changeList('remove', host);
+
+ callback();
+ });
+
+ peers[route] = socket;
+});
+
+function Filter(socket, options) {
+ this.options = options || {};
+ this.socket = socket;
+};
+Filter.prototype.test = function test(host) {
+ var filter = this.options;
+ var result;
+
+ if(filter['url'] && typeof host['url'] === 'string') {
+ try {
+ result = host['url'].match(filter['url']);
+ if(!result)
+ return true;
+ } catch(e) {
+ return true;
+ }
+ }
+
+ if(filter['metadata'] && host['metadata']) {
+ var metadataFilter = filter['metadata'];
+ var metadataHost = host['metadata'];
+
+ if(metadataFilter['name'] && typeof metadataHost['name'] === 'string') {
+ try {
+ result = metadataHost['name'].match(metadataFilter['name']);
+ if(!result)
+ return true;
+ } catch(e) {
+ return true;
+ }
+ }
+ }
+
+ return false;
+};
+
+var lists = {};
+
+function changeList(operation, host) {
+ var clients = Object.keys(lists);
+ clients.forEach(function(client) {
+ var filter = lists[client];
+ if(!host['listed'])
+ return;
+ if(!filter.test(host)) {
+ var data = operation === 'remove' ? host['route'] : host;
+ filter.socket.emit(operation, data);
+ }
+ });
+};
+
+io.of('/list').on('connection', function(socket) {
+ var id = socket['id'];
+
+ socket.on('disconnect', function() {
+ delete lists[id];
+ });
+
+ socket.on('list', function(options) {
+ var filter = new Filter(socket, options);
+
+ var result = [];
+
+ var hostIds = Object.keys(hosts);
+ hostIds.forEach(function(hostId) {
+ var host = hosts[hostId];
+ if(!host['listed'])
+ return;
+ if(!filter.test(host))
+ result.push(host);
+ });
+
+ lists[id] = filter;
+
+ socket.emit('truncate', result);
+ });
+});
diff --git a/tests/sockets/p2p/client/p2p-client.js b/tests/sockets/p2p/client/p2p-client.js
new file mode 100644
index 00000000..2c660210
--- /dev/null
+++ b/tests/sockets/p2p/client/p2p-client.js
@@ -0,0 +1,4485 @@
+
+;(function(define, global) { 'use strict';
+define(['module'], function(module) {
+
+ /*! Socket.IO.js build:0.9.11, development. Copyright(c) 2011 LearnBoost <dev@learnboost.com> MIT Licensed
+ Modified to work in-line; Removed Flash transport code */
+ var io = ('undefined' === typeof module ? {} : module.exports);
+ (function() {
+
+ /**
+ * socket.io
+ * Copyright(c) 2011 LearnBoost <dev@learnboost.com>
+ * MIT Licensed
+ */
+
+ (function (exports, global) {
+
+ /**
+ * IO namespace.
+ *
+ * @namespace
+ */
+
+ var io = exports;
+
+ /**
+ * Socket.IO version
+ *
+ * @api public
+ */
+
+ io.version = '0.9.11';
+
+ /**
+ * Protocol implemented.
+ *
+ * @api public
+ */
+
+ io.protocol = 1;
+
+ /**
+ * Available transports, these will be populated with the available transports
+ *
+ * @api public
+ */
+
+ io.transports = [];
+
+ /**
+ * Keep track of jsonp callbacks.
+ *
+ * @api private
+ */
+
+ io.j = [];
+
+ /**
+ * Keep track of our io.Sockets
+ *
+ * @api private
+ */
+ io.sockets = {};
+
+
+ /**
+ * Manages connections to hosts.
+ *
+ * @param {String} uri
+ * @Param {Boolean} force creation of new socket (defaults to false)
+ * @api public
+ */
+
+ io.connect = function (host, details) {
+ var uri = io.util.parseUri(host)
+ , uuri
+ , socket;
+
+ if (global && global.location) {
+ uri.protocol = uri.protocol || global.location.protocol.slice(0, -1);
+ uri.host = uri.host || (global.document
+ ? global.document.domain : global.location.hostname);
+ uri.port = uri.port || global.location.port;
+ }
+
+ uuri = io.util.uniqueUri(uri);
+
+ var options = {
+ host: uri.host
+ , secure: 'https' == uri.protocol
+ , port: uri.port || ('https' == uri.protocol ? 443 : 80)
+ , query: uri.query || ''
+ };
+
+ io.util.merge(options, details);
+
+ if (options['force new connection'] || !io.sockets[uuri]) {
+ socket = new io.Socket(options);
+ }
+
+ if (!options['force new connection'] && socket) {
+ io.sockets[uuri] = socket;
+ }
+
+ socket = socket || io.sockets[uuri];
+
+ // if path is different from '' or /
+ return socket.of(uri.path.length > 1 ? uri.path : '');
+ };
+
+ })('object' === typeof module ? module.exports : (io = {}), global);
+ /**
+ * socket.io
+ * Copyright(c) 2011 LearnBoost <dev@learnboost.com>
+ * MIT Licensed
+ */
+
+ (function (exports, global) {
+
+ /**
+ * Utilities namespace.
+ *
+ * @namespace
+ */
+
+ var util = exports.util = {};
+
+ /**
+ * Parses an URI
+ *
+ * @author Steven Levithan <stevenlevithan.com> (MIT license)
+ * @api public
+ */
+
+ var re = /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/;
+
+ var parts = ['source', 'protocol', 'authority', 'userInfo', 'user', 'password',
+ 'host', 'port', 'relative', 'path', 'directory', 'file', 'query',
+ 'anchor'];
+
+ util.parseUri = function (str) {
+ var m = re.exec(str || '')
+ , uri = {}
+ , i = 14;
+
+ while (i--) {
+ uri[parts[i]] = m[i] || '';
+ }
+
+ return uri;
+ };
+
+ /**
+ * Produces a unique url that identifies a Socket.IO connection.
+ *
+ * @param {Object} uri
+ * @api public
+ */
+
+ util.uniqueUri = function (uri) {
+ var protocol = uri.protocol
+ , host = uri.host
+ , port = uri.port;
+
+ if ('document' in global) {
+ host = host || document.domain;
+ port = port || (protocol == 'https'
+ && document.location.protocol !== 'https:' ? 443 : document.location.port);
+ } else {
+ host = host || 'localhost';
+
+ if (!port && protocol == 'https') {
+ port = 443;
+ }
+ }
+
+ return (protocol || 'http') + '://' + host + ':' + (port || 80);
+ };
+
+ /**
+ * Mergest 2 query strings in to once unique query string
+ *
+ * @param {String} base
+ * @param {String} addition
+ * @api public
+ */
+
+ util.query = function (base, addition) {
+ var query = util.chunkQuery(base || '')
+ , components = [];
+
+ util.merge(query, util.chunkQuery(addition || ''));
+ for (var part in query) {
+ if (query.hasOwnProperty(part)) {
+ components.push(part + '=' + query[part]);
+ }
+ }
+
+ return components.length ? '?' + components.join('&') : '';
+ };
+
+ /**
+ * Transforms a querystring in to an object
+ *
+ * @param {String} qs
+ * @api public
+ */
+
+ util.chunkQuery = function (qs) {
+ var query = {}
+ , params = qs.split('&')
+ , i = 0
+ , l = params.length
+ , kv;
+
+ for (; i < l; ++i) {
+ kv = params[i].split('=');
+ if (kv[0]) {
+ query[kv[0]] = kv[1];
+ }
+ }
+
+ return query;
+ };
+
+ /**
+ * Executes the given function when the page is loaded.
+ *
+ * io.util.load(function () { console.log('page loaded'); });
+ *
+ * @param {Function} fn
+ * @api public
+ */
+
+ var pageLoaded = false;
+
+ util.load = function (fn) {
+ if ('document' in global && document.readyState === 'complete' || pageLoaded) {
+ return fn();
+ }
+
+ util.on(global, 'load', fn, false);
+ };
+
+ /**
+ * Adds an event.
+ *
+ * @api private
+ */
+
+ util.on = function (element, event, fn, capture) {
+ if (element.attachEvent) {
+ element.attachEvent('on' + event, fn);
+ } else if (element.addEventListener) {
+ element.addEventListener(event, fn, capture);
+ }
+ };
+
+ /**
+ * Generates the correct `XMLHttpRequest` for regular and cross domain requests.
+ *
+ * @param {Boolean} [xdomain] Create a request that can be used cross domain.
+ * @returns {XMLHttpRequest|false} If we can create a XMLHttpRequest.
+ * @api private
+ */
+
+ util.request = function (xdomain) {
+
+ if (xdomain && 'undefined' != typeof XDomainRequest && !util.ua.hasCORS) {
+ return new XDomainRequest();
+ }
+
+ if ('undefined' != typeof XMLHttpRequest && (!xdomain || util.ua.hasCORS)) {
+ return new XMLHttpRequest();
+ }
+
+ if (!xdomain) {
+ try {
+ return new window[(['Active'].concat('Object').join('X'))]('Microsoft.XMLHTTP');
+ } catch(e) { }
+ }
+
+ return null;
+ };
+
+ /**
+ * XHR based transport constructor.
+ *
+ * @constructor
+ * @api public
+ */
+
+ /**
+ * Change the internal pageLoaded value.
+ */
+
+ if ('undefined' != typeof window) {
+ util.load(function () {
+ pageLoaded = true;
+ });
+ }
+
+ /**
+ * Defers a function to ensure a spinner is not displayed by the browser
+ *
+ * @param {Function} fn
+ * @api public
+ */
+
+ util.defer = function (fn) {
+ if (!util.ua.webkit || 'undefined' != typeof importScripts) {
+ return fn();
+ }
+
+ util.load(function () {
+ setTimeout(fn, 100);
+ });
+ };
+
+ /**
+ * Merges two objects.
+ *
+ * @api public
+ */
+
+ util.merge = function merge (target, additional, deep, lastseen) {
+ var seen = lastseen || []
+ , depth = typeof deep == 'undefined' ? 2 : deep
+ , prop;
+
+ for (prop in additional) {
+ if (additional.hasOwnProperty(prop) && util.indexOf(seen, prop) < 0) {
+ if (typeof target[prop] !== 'object' || !depth) {
+ target[prop] = additional[prop];
+ seen.push(additional[prop]);
+ } else {
+ util.merge(target[prop], additional[prop], depth - 1, seen);
+ }
+ }
+ }
+
+ return target;
+ };
+
+ /**
+ * Merges prototypes from objects
+ *
+ * @api public
+ */
+
+ util.mixin = function (ctor, ctor2) {
+ util.merge(ctor.prototype, ctor2.prototype);
+ };
+
+ /**
+ * Shortcut for prototypical and static inheritance.
+ *
+ * @api private
+ */
+
+ util.inherit = function (ctor, ctor2) {
+ function f() {};
+ f.prototype = ctor2.prototype;
+ ctor.prototype = new f;
+ };
+
+ /**
+ * Checks if the given object is an Array.
+ *
+ * io.util.isArray([]); // true
+ * io.util.isArray({}); // false
+ *
+ * @param Object obj
+ * @api public
+ */
+
+ util.isArray = Array.isArray || function (obj) {
+ return Object.prototype.toString.call(obj) === '[object Array]';
+ };
+
+ /**
+ * Intersects values of two arrays into a third
+ *
+ * @api public
+ */
+
+ util.intersect = function (arr, arr2) {
+ var ret = []
+ , longest = arr.length > arr2.length ? arr : arr2
+ , shortest = arr.length > arr2.length ? arr2 : arr;
+
+ for (var i = 0, l = shortest.length; i < l; i++) {
+ if (~util.indexOf(longest, shortest[i]))
+ ret.push(shortest[i]);
+ }
+
+ return ret;
+ };
+
+ /**
+ * Array indexOf compatibility.
+ *
+ * @see bit.ly/a5Dxa2
+ * @api public
+ */
+
+ util.indexOf = function (arr, o, i) {
+
+ for (var j = arr.length, i = i < 0 ? i + j < 0 ? 0 : i + j : i || 0;
+ i < j && arr[i] !== o; i++) {}
+
+ return j <= i ? -1 : i;
+ };
+
+ /**
+ * Converts enumerables to array.
+ *
+ * @api public
+ */
+
+ util.toArray = function (enu) {
+ var arr = [];
+
+ for (var i = 0, l = enu.length; i < l; i++)
+ arr.push(enu[i]);
+
+ return arr;
+ };
+
+ /**
+ * UA / engines detection namespace.
+ *
+ * @namespace
+ */
+
+ util.ua = {};
+
+ /**
+ * Whether the UA supports CORS for XHR.
+ *
+ * @api public
+ */
+
+ util.ua.hasCORS = 'undefined' != typeof XMLHttpRequest && (function () {
+ try {
+ var a = new XMLHttpRequest();
+ } catch (e) {
+ return false;
+ }
+
+ return a.withCredentials != undefined;
+ })();
+
+ /**
+ * Detect webkit.
+ *
+ * @api public
+ */
+
+ util.ua.webkit = 'undefined' != typeof navigator
+ && /webkit/i.test(navigator.userAgent);
+
+ /**
+ * Detect iPad/iPhone/iPod.
+ *
+ * @api public
+ */
+
+ util.ua.iDevice = 'undefined' != typeof navigator
+ && /iPad|iPhone|iPod/i.test(navigator.userAgent);
+
+ })('undefined' != typeof io ? io : module.exports, global);
+ /**
+ * socket.io
+ * Copyright(c) 2011 LearnBoost <dev@learnboost.com>
+ * MIT Licensed
+ */
+
+ (function (exports, io) {
+
+ /**
+ * Expose constructor.
+ */
+
+ exports.EventEmitter = EventEmitter;
+
+ /**
+ * Event emitter constructor.
+ *
+ * @api public.
+ */
+
+ function EventEmitter () {};
+
+ /**
+ * Adds a listener
+ *
+ * @api public
+ */
+
+ EventEmitter.prototype.on = function (name, fn) {
+ if (!this.$events) {
+ this.$events = {};
+ }
+
+ if (!this.$events[name]) {
+ this.$events[name] = fn;
+ } else if (io.util.isArray(this.$events[name])) {
+ this.$events[name].push(fn);
+ } else {
+ this.$events[name] = [this.$events[name], fn];
+ }
+
+ return this;
+ };
+
+ EventEmitter.prototype.addListener = EventEmitter.prototype.on;
+
+ /**
+ * Adds a volatile listener.
+ *
+ * @api public
+ */
+
+ EventEmitter.prototype.once = function (name, fn) {
+ var self = this;
+
+ function on () {
+ self.removeListener(name, on);
+ fn.apply(this, arguments);
+ };
+
+ on.listener = fn;
+ this.on(name, on);
+
+ return this;
+ };
+
+ /**
+ * Removes a listener.
+ *
+ * @api public
+ */
+
+ EventEmitter.prototype.removeListener = function (name, fn) {
+ if (this.$events && this.$events[name]) {
+ var list = this.$events[name];
+
+ if (io.util.isArray(list)) {
+ var pos = -1;
+
+ for (var i = 0, l = list.length; i < l; i++) {
+ if (list[i] === fn || (list[i].listener && list[i].listener === fn)) {
+ pos = i;
+ break;
+ }
+ }
+
+ if (pos < 0) {
+ return this;
+ }
+
+ list.splice(pos, 1);
+
+ if (!list.length) {
+ delete this.$events[name];
+ }
+ } else if (list === fn || (list.listener && list.listener === fn)) {
+ delete this.$events[name];
+ }
+ }
+
+ return this;
+ };
+
+ /**
+ * Removes all listeners for an event.
+ *
+ * @api public
+ */
+
+ EventEmitter.prototype.removeAllListeners = function (name) {
+ if (name === undefined) {
+ this.$events = {};
+ return this;
+ }
+
+ if (this.$events && this.$events[name]) {
+ this.$events[name] = null;
+ }
+
+ return this;
+ };
+
+ /**
+ * Gets all listeners for a certain event.
+ *
+ * @api publci
+ */
+
+ EventEmitter.prototype.listeners = function (name) {
+ if (!this.$events) {
+ this.$events = {};
+ }
+
+ if (!this.$events[name]) {
+ this.$events[name] = [];
+ }
+
+ if (!io.util.isArray(this.$events[name])) {
+ this.$events[name] = [this.$events[name]];
+ }
+
+ return this.$events[name];
+ };
+
+ /**
+ * Emits an event.
+ *
+ * @api public
+ */
+
+ EventEmitter.prototype.emit = function (name) {
+ if (!this.$events) {
+ return false;
+ }
+
+ var handler = this.$events[name];
+
+ if (!handler) {
+ return false;
+ }
+
+ var args = Array.prototype.slice.call(arguments, 1);
+
+ if ('function' == typeof handler) {
+ handler.apply(this, args);
+ } else if (io.util.isArray(handler)) {
+ var listeners = handler.slice();
+
+ for (var i = 0, l = listeners.length; i < l; i++) {
+ listeners[i].apply(this, args);
+ }
+ } else {
+ return false;
+ }
+
+ return true;
+ };
+
+ })(
+ 'undefined' != typeof io ? io : module.exports
+ , 'undefined' != typeof io ? io : module.parent.exports
+ );
+
+ /**
+ * socket.io
+ * Copyright(c) 2011 LearnBoost <dev@learnboost.com>
+ * MIT Licensed
+ */
+
+ /**
+ * Based on JSON2 (http://www.JSON.org/js.html).
+ */
+
+ (function (exports, nativeJSON) {
+ "use strict";
+
+ // use native JSON if it's available
+ if (nativeJSON && nativeJSON.parse){
+ return exports.JSON = {
+ parse: nativeJSON.parse
+ , stringify: nativeJSON.stringify
+ };
+ }
+
+ var JSON = exports.JSON = {};
+
+ function f(n) {
+ // Format integers to have at least two digits.
+ return n < 10 ? '0' + n : n;
+ }
+
+ function date(d, key) {
+ return isFinite(d.valueOf()) ?
+ d.getUTCFullYear() + '-' +
+ f(d.getUTCMonth() + 1) + '-' +
+ f(d.getUTCDate()) + 'T' +
+ f(d.getUTCHours()) + ':' +
+ f(d.getUTCMinutes()) + ':' +
+ f(d.getUTCSeconds()) + 'Z' : null;
+ };
+
+ var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
+ escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
+ gap,
+ indent,
+ meta = { // table of character substitutions
+ '\b': '\\b',
+ '\t': '\\t',
+ '\n': '\\n',
+ '\f': '\\f',
+ '\r': '\\r',
+ '"' : '\\"',
+ '\\': '\\\\'
+ },
+ rep;
+
+
+ function quote(string) {
+
+ // If the string contains no control characters, no quote characters, and no
+ // backslash characters, then we can safely slap some quotes around it.
+ // Otherwise we must also replace the offending characters with safe escape
+ // sequences.
+
+ escapable.lastIndex = 0;
+ return escapable.test(string) ? '"' + string.replace(escapable, function (a) {
+ var c = meta[a];
+ return typeof c === 'string' ? c :
+ '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
+ }) + '"' : '"' + string + '"';
+ }
+
+
+ function str(key, holder) {
+
+ // Produce a string from holder[key].
+
+ var i, // The loop counter.
+ k, // The member key.
+ v, // The member value.
+ length,
+ mind = gap,
+ partial,
+ value = holder[key];
+
+ // If the value has a toJSON method, call it to obtain a replacement value.
+
+ if (value instanceof Date) {
+ value = date(key);
+ }
+
+ // If we were called with a replacer function, then call the replacer to
+ // obtain a replacement value.
+
+ if (typeof rep === 'function') {
+ value = rep.call(holder, key, value);
+ }
+
+ // What happens next depends on the value's type.
+
+ switch (typeof value) {
+ case 'string':
+ return quote(value);
+
+ case 'number':
+
+ // JSON numbers must be finite. Encode non-finite numbers as null.
+
+ return isFinite(value) ? String(value) : 'null';
+
+ case 'boolean':
+ case 'null':
+
+ // If the value is a boolean or null, convert it to a string. Note:
+ // typeof null does not produce 'null'. The case is included here in
+ // the remote chance that this gets fixed someday.
+
+ return String(value);
+
+ // If the type is 'object', we might be dealing with an object or an array or
+ // null.
+
+ case 'object':
+
+ // Due to a specification blunder in ECMAScript, typeof null is 'object',
+ // so watch out for that case.
+
+ if (!value) {
+ return 'null';
+ }
+
+ // Make an array to hold the partial results of stringifying this object value.
+
+ gap += indent;
+ partial = [];
+
+ // Is the value an array?
+
+ if (Object.prototype.toString.apply(value) === '[object Array]') {
+
+ // The value is an array. Stringify every element. Use null as a placeholder
+ // for non-JSON values.
+
+ length = value.length;
+ for (i = 0; i < length; i += 1) {
+ partial[i] = str(i, value) || 'nul