flashsocket.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*!
  2. * socket.io-node
  3. * Copyright(c) 2011 LearnBoost <dev@learnboost.com>
  4. * MIT Licensed
  5. */
  6. /**
  7. * Module requirements.
  8. */
  9. var WebSocket = require('./websocket');
  10. /**
  11. * Export the constructor.
  12. */
  13. exports = module.exports = FlashSocket;
  14. /**
  15. * The FlashSocket transport is just a proxy
  16. * for WebSocket connections.
  17. *
  18. * @api public
  19. */
  20. function FlashSocket (mng, data, req) {
  21. return WebSocket.call(this, mng, data, req);
  22. }
  23. /**
  24. * Inherits from WebSocket.
  25. */
  26. FlashSocket.prototype.__proto__ = WebSocket.prototype;
  27. /**
  28. * Transport name
  29. *
  30. * @api public
  31. */
  32. FlashSocket.prototype.name = 'flashsocket';
  33. /**
  34. * Listens for new configuration changes of the Manager
  35. * this way we can enable and disable the flash server.
  36. *
  37. * @param {Manager} Manager instance.
  38. * @api private
  39. */
  40. FlashSocket.init = function (manager) {
  41. var server;
  42. function create () {
  43. // Drop out immediately if the user has
  44. // disabled the flash policy server
  45. if (!manager.get('flash policy server')) {
  46. return;
  47. }
  48. server = require('policyfile').createServer({
  49. log: function(msg){
  50. manager.log.info(msg);
  51. }
  52. }, manager.get('origins'));
  53. server.on('close', function (e) {
  54. server = null;
  55. });
  56. server.listen(manager.get('flash policy port'), manager.server);
  57. manager.flashPolicyServer = server;
  58. }
  59. // listen for origin changes, so we can update the server
  60. manager.on('set:origins', function (value, key) {
  61. if (!server) return;
  62. // update the origins and compile a new response buffer
  63. server.origins = Array.isArray(value) ? value : [value];
  64. server.compile();
  65. });
  66. // destory the server and create a new server
  67. manager.on('set:flash policy port', function (value, key) {
  68. var transports = manager.get('transports');
  69. if (~transports.indexOf('flashsocket')) {
  70. if (server) {
  71. if (server.port === value) return;
  72. // destroy the server and rebuild it on a new port
  73. try {
  74. server.close();
  75. }
  76. catch (e) { /* ignore exception. could e.g. be that the server isn't started yet */ }
  77. }
  78. create();
  79. }
  80. });
  81. // create or destroy the server
  82. manager.on('set:flash policy server', function (value, key) {
  83. var transports = manager.get('transports');
  84. if (~transports.indexOf('flashsocket')) {
  85. if (server && !value) {
  86. // destroy the server
  87. try {
  88. server.close();
  89. }
  90. catch (e) { /* ignore exception. could e.g. be that the server isn't started yet */ }
  91. }
  92. } else if (!server && value) {
  93. // create the server
  94. create();
  95. }
  96. });
  97. // only start the server
  98. manager.on('set:transports', function (value, key){
  99. if (!server && ~manager.get('transports').indexOf('flashsocket')) {
  100. create();
  101. }
  102. });
  103. // check if we need to initialize at start
  104. if (~manager.get('transports').indexOf('flashsocket')){
  105. create();
  106. }
  107. };