summaryrefslogtreecommitdiff
path: root/node_modules/socket.io/lib/transports/jsonp-polling.js
diff options
context:
space:
mode:
authorJules Laplace <jules@okfoc.us>2012-09-24 16:22:07 -0400
committerJules Laplace <jules@okfoc.us>2012-09-24 16:22:07 -0400
commit686106d544ecc3b6ffd4db2b665d3bc879a58d8c (patch)
treea5b5e50237cef70e12f0745371896e96f5f6d578 /node_modules/socket.io/lib/transports/jsonp-polling.js
ok
Diffstat (limited to 'node_modules/socket.io/lib/transports/jsonp-polling.js')
-rw-r--r--node_modules/socket.io/lib/transports/jsonp-polling.js96
1 files changed, 96 insertions, 0 deletions
diff --git a/node_modules/socket.io/lib/transports/jsonp-polling.js b/node_modules/socket.io/lib/transports/jsonp-polling.js
new file mode 100644
index 0000000..83d11b8
--- /dev/null
+++ b/node_modules/socket.io/lib/transports/jsonp-polling.js
@@ -0,0 +1,96 @@
+
+/*!
+ * socket.io-node
+ * Copyright(c) 2011 LearnBoost <dev@learnboost.com>
+ * MIT Licensed
+ */
+
+/**
+ * Module requirements.
+ */
+
+var HTTPPolling = require('./http-polling');
+
+/**
+ * Export the constructor.
+ */
+
+exports = module.exports = JSONPPolling;
+
+/**
+ * JSON-P polling transport.
+ *
+ * @api public
+ */
+
+function JSONPPolling (mng, data, req) {
+ HTTPPolling.call(this, mng, data, req);
+
+ this.head = 'io.j[0](';
+ this.foot = ');';
+
+ if (data.query.i) {
+ this.head = 'io.j[' + data.query.i + '](';
+ }
+};
+
+/**
+ * Inherits from Transport.
+ */
+
+JSONPPolling.prototype.__proto__ = HTTPPolling.prototype;
+
+/**
+ * Transport name
+ *
+ * @api public
+ */
+
+JSONPPolling.prototype.name = 'jsonppolling';
+
+/**
+ * Make sure POST are decoded.
+ */
+
+JSONPPolling.prototype.postEncoded = true;
+
+/**
+ * Handles incoming data.
+ * Due to a bug in \n handling by browsers, we expect a JSONified string.
+ *
+ * @api private
+ */
+
+JSONPPolling.prototype.onData = function (data) {
+ try {
+ data = JSON.parse(data);
+ } catch (e) {
+ this.error('parse', 'reconnect');
+ return;
+ }
+
+ HTTPPolling.prototype.onData.call(this, data);
+};
+
+/**
+ * Performs the write.
+ *
+ * @api private
+ */
+
+JSONPPolling.prototype.doWrite = function (data) {
+ HTTPPolling.prototype.doWrite.call(this);
+
+ var data = data === undefined
+ ? '' : this.head + JSON.stringify(data) + this.foot;
+
+ this.response.writeHead(200, {
+ 'Content-Type': 'text/javascript; charset=UTF-8'
+ , 'Content-Length': Buffer.byteLength(data)
+ , 'Connection': 'Keep-Alive'
+ , 'X-XSS-Protection': '0'
+ });
+
+ this.response.write(data);
+ this.log.debug(this.name + ' writing', data);
+};