6 var WS = require('./websocket')
7 , util = require('../util')
8 , debug = require('debug')('engine.io-client:flashsocket');
14 module.exports = FlashWS;
17 * Obfuscated key for Blue Coat.
20 var xobject = global[['Active'].concat('Object').join('X')];
23 * FlashWS constructor.
28 function FlashWS (options) {
29 WS.call(this, options);
30 this.flashPath = options.flashPath;
31 this.policyPort = options.policyPort;
35 * Inherits from WebSocket.
38 util.inherits(FlashWS, WS);
46 FlashWS.prototype.name = 'flashsocket';
49 * Opens the transport.
54 FlashWS.prototype.doOpen = function () {
56 // let the probe timeout
60 // instrument websocketjs logging
63 var str = Array.prototype.join.call(arguments, ' ');
64 debug('[websocketjs %s] %s', type, str);
68 WEB_SOCKET_LOGGER = { log: log('debug'), error: log('error') };
69 WEB_SOCKET_SUPPRESS_CROSS_DOMAIN_SWF_ERROR = true;
70 WEB_SOCKET_DISABLE_AUTO_INITIALIZATION = true;
72 if ('undefined' == typeof WEB_SOCKET_SWF_LOCATION) {
73 WEB_SOCKET_SWF_LOCATION = this.flashPath + 'WebSocketMainInsecure.swf';
77 var deps = [this.flashPath + 'web_socket.js'];
79 if ('undefined' == typeof swfobject) {
80 deps.unshift(this.flashPath + 'swfobject.js');
85 load(deps, function () {
86 self.ready(function () {
87 WebSocket.__addTask(function () {
88 WS.prototype.doOpen.call(self);
95 * Override to prevent closing uninitialized flashsocket.
100 FlashWS.prototype.doClose = function () {
101 if (!this.socket) return;
103 WebSocket.__addTask(function() {
104 WS.prototype.doClose.call(self);
109 * Writes to the Flash socket.
114 FlashWS.prototype.write = function() {
115 var self = this, args = arguments;
116 WebSocket.__addTask(function () {
117 WS.prototype.write.apply(self, args);
122 * Called upon dependencies are loaded.
127 FlashWS.prototype.ready = function (fn) {
128 if (typeof WebSocket == 'undefined' ||
129 !('__initialize' in WebSocket) || !swfobject) {
133 if (swfobject.getFlashPlayerVersion().major < 10) {
138 // Only start downloading the swf file when the checked that this browser
139 // actually supports it
140 if (!FlashWS.loaded) {
141 if (843 != self.policyPort) {
142 WebSocket.loadFlashPolicyFile('xmlsocket://' + self.host + ':' + self.policyPort);
145 WebSocket.__initialize();
146 FlashWS.loaded = true;
161 * Feature detection for flashsocket.
163 * @return {Boolean} whether this transport is available.
167 FlashWS.prototype.check = function () {
168 if ('undefined' != typeof process) {
172 if (typeof WebSocket != 'undefined' && !('__initialize' in WebSocket)) {
179 control = new xobject('ShockwaveFlash.ShockwaveFlash');
185 for (var i = 0, l = navigator.plugins.length; i < l; i++) {
186 for (var j = 0, m = navigator.plugins[i].length; j < m; j++) {
187 if (navigator.plugins[i][j].description == 'Shockwave Flash') {
198 * Lazy loading of scripts.
199 * Based on $script by Dustin Diaz - MIT
205 * Injects a script. Keeps tracked of injected ones.
207 * @param {String} path
208 * @param {Function} callback
212 function create (path, fn) {
213 if (scripts[path]) return fn();
215 var el = document.createElement('script');
218 debug('loading "%s"', path);
219 el.onload = el.onreadystatechange = function () {
220 if (loaded || scripts[path]) return;
221 var rs = el.readyState;
222 if (!rs || 'loaded' == rs || 'complete' == rs) {
223 debug('loaded "%s"', path);
224 el.onload = el.onreadystatechange = null;
226 scripts[path] = true;
234 var head = document.getElementsByTagName('head')[0];
235 head.insertBefore(el, head.firstChild);
239 * Loads scripts and fires a callback.
241 * @param {Array} paths
242 * @param {Function} callback
245 function load (arr, fn) {
246 function process (i) {
247 if (!arr[i]) return fn();
248 create(arr[i], function () {