PageRenderTime 257ms CodeModel.GetById 167ms app.highlight 4ms RepoModel.GetById 84ms app.codeStats 0ms

/js/lib/Socket.IO-node/lib/socket.io/transports/jsonp-polling.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 77 lines | 31 code | 16 blank | 30 comment | 4 complexity | 137d74e4247636344f6dca13be17abe4 MD5 | raw file
 1
 2/*!
 3 * Socket.IO - transports - JSONPPolling
 4 * Copyright (c) 2010-2011 Guillermo Rauch <guillermo@learnboost.com>
 5 * MIT Licensed
 6 */
 7
 8var XHRPolling = require('./xhr-polling');
 9
10/**
11 * Expose `JSONPPolling`.
12 */
13
14module.exports = JSONPPolling;
15
16/**
17 * Initialize a `JSONPPolling` client.
18 *
19 * @api private
20 */
21
22function JSONPPolling() {
23  XHRPolling.apply(this, arguments);
24};
25
26/**
27 * Inherit from `XHRPolling.prototype`.
28 */
29
30JSONPPolling.prototype.__proto__ = XHRPolling.prototype;
31
32/**
33 * Options.
34 */
35
36JSONPPolling.prototype.options = {
37    timeout: null
38  , closeTimeout: 8000
39  , duration: 20000
40};
41
42/**
43 * Connection implementation.
44 * 
45 * @api private
46 */
47  
48JSONPPolling.prototype._onConnect = function(req, res){
49  this._index = req.url.match(/\/([0-9]+)\/?$/).pop();
50  XHRPolling.prototype._onConnect.call(this, req, res);
51};
52
53/**
54 * Write implementation.
55 * 
56 * @param {String} message
57 * @api private
58 */
59
60JSONPPolling.prototype._write = function(message){
61  if (this._open){
62    var req = this.request
63      , res = this.response
64      , origin = req.headers.origin;
65
66    if (origin && !this._verifyOrigin(origin)){
67      message = "alert('Cross domain security restrictions not met');";
68    } else {
69      message = "io.JSONP["+ this._index +"]._("+ JSON.stringify(message) +");";
70    }
71
72    res.setHeader('Content-Type', 'text/javascript; charset=UTF-8');
73    res.setHeader('Content-Length', Buffer.byteLength(message));
74    res.end(message);
75    this._onClose();
76  }
77};