PageRenderTime 162ms CodeModel.GetById 80ms app.highlight 6ms RepoModel.GetById 75ms app.codeStats 0ms

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

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 132 lines | 70 code | 24 blank | 38 comment | 11 complexity | 297e2bc0fbf243691772bb95013dd80b MD5 | raw file
  1
  2/*!
  3 * Socket.IO - transports - Polling
  4 * Copyright (c) 2010-2011 Guillermo Rauch <guillermo@learnboost.com>
  5 * MIT Licensed
  6 */
  7
  8var Client = require('../client')
  9  , qs = require('querystring');
 10
 11/**
 12 * Expose `Polling`.
 13 */
 14
 15module.exports = Polling;
 16
 17/**
 18 * Initialize a `Polling` client.
 19 *
 20 * @api private
 21 */
 22
 23function Polling() {
 24  Client.apply(this, arguments);
 25};
 26
 27/**
 28 * Inherit from `Client.prototype`.
 29 */
 30
 31Polling.prototype.__proto__ = Client.prototype;
 32
 33/**
 34 * Options.
 35 */
 36
 37Polling.prototype.options = {
 38    timeout: null
 39  , closeTimeout: 8000
 40  , duration: 20000
 41};
 42
 43/**
 44 * Connection implementation.
 45 * 
 46 * @api private
 47 */
 48
 49Polling.prototype._onConnect = function(req, res){
 50  var self = this
 51    , body = '';
 52
 53  switch (req.method){
 54    case 'GET':
 55      Client.prototype._onConnect.call(this, req, res);
 56      this._closeTimeout = setTimeout(function(){
 57        self._write('');
 58      }, this.duration);
 59      this._payload();
 60      break;
 61      
 62    case 'POST':
 63      req.on('data', function(chunk){ body += chunk; });
 64      req.on('end', function(){
 65        res.setHeader('Content-Type', 'text/plain');
 66
 67        if (req.headers.origin){
 68          if (self._verifyOrigin(req.headers.origin)){
 69            res.setHeader('Access-Control-Allow-Origin', '*');
 70            if (req.headers.cookie) {
 71              res.setHeader('Access-Control-Allow-Credentials', 'true');
 72            }
 73          } else {
 74            res.statusCode = 401;
 75            res.end('unauthorized');
 76            return;
 77          }
 78        }
 79
 80        try {
 81          // optimization: just strip first 5 characters here?
 82          //  ^ totally
 83          var msg = qs.parse(body);
 84          self._onMessage(msg.data);
 85        } catch(e){
 86          self.listener.log('xhr-polling message handler error - ' + e.stack);
 87        }
 88
 89        res.end('ok');
 90      });
 91      break;
 92  }
 93};
 94
 95/**
 96 * Close Implementation.
 97 * 
 98 * @api private
 99 */
100
101Polling.prototype._onClose = function(){
102  if (this._closeTimeout) clearTimeout(this._closeTimeout);
103  return Client.prototype._onClose.call(this);
104};
105
106/**
107 * Write implementation.
108 *
109 * @param {String} message
110 * @api private
111 */
112  
113Polling.prototype._write = function(message){
114  if (this._open) {
115    var res = this.response
116      , req = this.request
117      , origin = req.headers.origin;
118
119    res.setHeader('Content-Type', 'text/plain; charset=UTF-8');
120    res.setHeader('Content-Length', Buffer.byteLength(message));
121
122    // https://developer.mozilla.org/En/HTTP_Access_Control
123    if (origin && this._verifyOrigin(origin)){
124      origin = 'null' == origin ? '*' : origin;
125      res.setHeader('Access-Control-Allow-Origin', origin);
126      if (req.headers.cookie) res.setHeader('Access-Control-Allow-Credentials', 'true');
127    }
128
129    res.end(message);
130    this._onClose();
131  }
132};