flashsocket.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /**
  2. * socket.io
  3. * Copyright(c) 2011 LearnBoost <dev@learnboost.com>
  4. * MIT Licensed
  5. */
  6. (function (exports, io) {
  7. /**
  8. * Expose constructor.
  9. */
  10. exports.flashsocket = Flashsocket;
  11. /**
  12. * The FlashSocket transport. This is a API wrapper for the HTML5 WebSocket
  13. * specification. It uses a .swf file to communicate with the server. If you want
  14. * to serve the .swf file from a other server than where the Socket.IO script is
  15. * coming from you need to use the insecure version of the .swf. More information
  16. * about this can be found on the github page.
  17. *
  18. * @constructor
  19. * @extends {io.Transport.websocket}
  20. * @api public
  21. */
  22. function Flashsocket () {
  23. io.Transport.websocket.apply(this, arguments);
  24. };
  25. /**
  26. * Inherits from Transport.
  27. */
  28. io.util.inherit(Flashsocket, io.Transport.websocket);
  29. /**
  30. * Transport name
  31. *
  32. * @api public
  33. */
  34. Flashsocket.prototype.name = 'flashsocket';
  35. /**
  36. * Disconnect the established `FlashSocket` connection. This is done by adding a
  37. * new task to the FlashSocket. The rest will be handled off by the `WebSocket`
  38. * transport.
  39. *
  40. * @returns {Transport}
  41. * @api public
  42. */
  43. Flashsocket.prototype.open = function () {
  44. var self = this
  45. , args = arguments;
  46. WebSocket.__addTask(function () {
  47. io.Transport.websocket.prototype.open.apply(self, args);
  48. });
  49. return this;
  50. };
  51. /**
  52. * Sends a message to the Socket.IO server. This is done by adding a new
  53. * task to the FlashSocket. The rest will be handled off by the `WebSocket`
  54. * transport.
  55. *
  56. * @returns {Transport}
  57. * @api public
  58. */
  59. Flashsocket.prototype.send = function () {
  60. var self = this, args = arguments;
  61. WebSocket.__addTask(function () {
  62. io.Transport.websocket.prototype.send.apply(self, args);
  63. });
  64. return this;
  65. };
  66. /**
  67. * Disconnects the established `FlashSocket` connection.
  68. *
  69. * @returns {Transport}
  70. * @api public
  71. */
  72. Flashsocket.prototype.close = function () {
  73. WebSocket.__tasks.length = 0;
  74. io.Transport.websocket.prototype.close.call(this);
  75. return this;
  76. };
  77. /**
  78. * The WebSocket fall back needs to append the flash container to the body
  79. * element, so we need to make sure we have access to it. Or defer the call
  80. * until we are sure there is a body element.
  81. *
  82. * @param {Socket} socket The socket instance that needs a transport
  83. * @param {Function} fn The callback
  84. * @api private
  85. */
  86. Flashsocket.prototype.ready = function (socket, fn) {
  87. function init () {
  88. var options = socket.options
  89. , port = options['flash policy port']
  90. , path = [
  91. 'http' + (options.secure ? 's' : '') + ':/'
  92. , options.host + ':' + options.port
  93. , options.resource
  94. , 'static/flashsocket'
  95. , 'WebSocketMain' + (socket.isXDomain() ? 'Insecure' : '') + '.swf'
  96. ];
  97. // Only start downloading the swf file when the checked that this browser
  98. // actually supports it
  99. if (!Flashsocket.loaded) {
  100. if (typeof WEB_SOCKET_SWF_LOCATION === 'undefined') {
  101. // Set the correct file based on the XDomain settings
  102. WEB_SOCKET_SWF_LOCATION = path.join('/');
  103. }
  104. if (port !== 843) {
  105. WebSocket.loadFlashPolicyFile('xmlsocket://' + options.host + ':' + port);
  106. }
  107. WebSocket.__initialize();
  108. Flashsocket.loaded = true;
  109. }
  110. fn.call(self);
  111. }
  112. var self = this;
  113. if (document.body) return init();
  114. io.util.load(init);
  115. };
  116. /**
  117. * Check if the FlashSocket transport is supported as it requires that the Adobe
  118. * Flash Player plug-in version `10.0.0` or greater is installed. And also check if
  119. * the polyfill is correctly loaded.
  120. *
  121. * @returns {Boolean}
  122. * @api public
  123. */
  124. Flashsocket.check = function () {
  125. if (
  126. typeof WebSocket == 'undefined'
  127. || !('__initialize' in WebSocket) || !swfobject
  128. ) return false;
  129. return swfobject.getFlashPlayerVersion().major >= 10;
  130. };
  131. /**
  132. * Check if the FlashSocket transport can be used as cross domain / cross origin
  133. * transport. Because we can't see which type (secure or insecure) of .swf is used
  134. * we will just return true.
  135. *
  136. * @returns {Boolean}
  137. * @api public
  138. */
  139. Flashsocket.xdomainCheck = function () {
  140. return true;
  141. };
  142. /**
  143. * Disable AUTO_INITIALIZATION
  144. */
  145. if (typeof window != 'undefined') {
  146. WEB_SOCKET_DISABLE_AUTO_INITIALIZATION = true;
  147. }
  148. /**
  149. * Add the transport to your public io.transports array.
  150. *
  151. * @api private
  152. */
  153. io.transports.push('flashsocket');
  154. })(
  155. 'undefined' != typeof io ? io.Transport : module.exports
  156. , 'undefined' != typeof io ? io : module.parent.exports
  157. );