PageRenderTime 500ms CodeModel.GetById 151ms app.highlight 7ms RepoModel.GetById 108ms app.codeStats 0ms

/js/lib/Socket.IO-node/lib/socket.io/transports/flashsocket.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 116 lines | 74 code | 21 blank | 21 comment | 19 complexity | 66c6eed24e85990f937736d5657e9184 MD5 | raw file
  1
  2/*!
  3 * Socket.IO - transports - Flashsocket
  4 * Copyright (c) 2010-2011 Guillermo Rauch <guillermo@learnboost.com>
  5 * MIT Licensed
  6 */
  7
  8var net = require('net')
  9  , WebSocket = require('./websocket')
 10  , listeners = []
 11  , netserver;
 12
 13/**
 14 * Expose `Flashsocket`.
 15 */
 16
 17module.exports = Flashsocket;
 18
 19/**
 20 * Initialize a `Flashsocket`.
 21 *
 22 * @api private
 23 */
 24
 25function Flashsocket() {
 26  WebSocket.apply(this, arguments);
 27};
 28
 29/**
 30 * Inherit from `WebSocket.prototype`.
 31 */
 32
 33Flashsocket.prototype.__proto__ = WebSocket.prototype;
 34
 35/**
 36 * Requires upgrade.
 37 */
 38
 39Flashsocket.httpUpgrade = true;
 40
 41Flashsocket.init = function(listener){
 42  listeners.push(listener);
 43  
 44  listener.server.on('close', function(){
 45    listeners.splice(listeners.indexOf(listener), 1);
 46
 47    if (listeners.length === 0 && netserver){
 48      try {
 49        netserver.close();
 50      } catch(e){
 51        listener.log('flashsocket netserver close error - ' + e.stack)
 52      }
 53    }
 54  });
 55  
 56  if (listener.flashPolicyServer && netserver === undefined){
 57    netserver = net.createServer(function(socket){
 58      socket.addListener('error', function(err){
 59        if (socket && socket.end){
 60          socket.end();
 61          socket.destroy();
 62        }
 63      });
 64
 65      if (socket && socket.readyState == 'open') {
 66        socket.end(policy(listeners));
 67      }
 68    });
 69    
 70    try {
 71      netserver.listen(843);
 72    } catch(e){
 73      if (e.errno == 13)
 74        listener.log(
 75          'Your node instance does not have root privileges. ' 
 76          + 'This means that the flash XML policy file will be '
 77          + 'served inline instead of on port 843. This will slow '
 78          + 'down initial connections slightly.');
 79      netserver = null;
 80    }
 81  }
 82  
 83  // Could not listen on port 843 so policy requests will be inline
 84  listener.server.on('connection', function(stream){
 85    var flashCheck = function (data) {
 86      // Only check the initial data
 87      stream.removeListener("data", flashCheck);
 88      if (data[0] === 60 && data.length == 23) {
 89        if (data == '<policy-file-request/>\0') {
 90          listener.log("Answering flash policy request inline");
 91          if (stream && stream.readyState == 'open'){
 92            var xml = policy([listener]);
 93            stream.end(xml);
 94          }
 95        }
 96      }
 97    };
 98    
 99    stream.on('data', flashCheck);
100  });
101};
102
103function policy(listeners) {
104  var xml = '<?xml version="1.0"?>\n<!DOCTYPE cross-domain-policy SYSTEM'
105          + ' "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">\n<cross-domain-policy>\n';
106
107  listeners.forEach(function(l){
108    [].concat(l.origins).forEach(function(origin){
109      var parts = origin.split(':');
110      xml += '<allow-access-from domain="' + parts[0] + '" to-ports="'+ parts[1] +'"/>\n';
111    });
112  });
113
114  xml += '</cross-domain-policy>\n';
115  return xml;
116};