4 * Copyright(c) 2011 LearnBoost <dev@learnboost.com>
8 (function (exports, io) {
14 exports.flashsocket = Flashsocket;
17 * The FlashSocket transport. This is a API wrapper for the HTML5 WebSocket
18 * specification. It uses a .swf file to communicate with the server. If you want
19 * to serve the .swf file from a other server than where the Socket.IO script is
20 * coming from you need to use the insecure version of the .swf. More information
21 * about this can be found on the github page.
24 * @extends {io.Transport.websocket}
28 function Flashsocket () {
29 io.Transport.websocket.apply(this, arguments);
33 * Inherits from Transport.
36 io.util.inherit(Flashsocket, io.Transport.websocket);
44 Flashsocket.prototype.name = 'flashsocket';
47 * Disconnect the established `FlashSocket` connection. This is done by adding a
48 * new task to the FlashSocket. The rest will be handled off by the `WebSocket`
51 * @returns {Transport}
55 Flashsocket.prototype.open = function () {
59 WebSocket.__addTask(function () {
60 io.Transport.websocket.prototype.open.apply(self, args);
66 * Sends a message to the Socket.IO server. This is done by adding a new
67 * task to the FlashSocket. The rest will be handled off by the `WebSocket`
70 * @returns {Transport}
74 Flashsocket.prototype.send = function () {
75 var self = this, args = arguments;
76 WebSocket.__addTask(function () {
77 io.Transport.websocket.prototype.send.apply(self, args);
83 * Disconnects the established `FlashSocket` connection.
85 * @returns {Transport}
89 Flashsocket.prototype.close = function () {
90 WebSocket.__tasks.length = 0;
91 io.Transport.websocket.prototype.close.call(this);
96 * The WebSocket fall back needs to append the flash container to the body
97 * element, so we need to make sure we have access to it. Or defer the call
98 * until we are sure there is a body element.
100 * @param {Socket} socket The socket instance that needs a transport
101 * @param {Function} fn The callback
105 Flashsocket.prototype.ready = function (socket, fn) {
107 var options = socket.options
108 , port = options['flash policy port']
110 'http' + (options.secure ? 's' : '') + ':/'
111 , options.host + ':' + options.port
113 , 'static/flashsocket'
114 , 'WebSocketMain' + (socket.isXDomain() ? 'Insecure' : '') + '.swf'
117 // Only start downloading the swf file when the checked that this browser
118 // actually supports it
119 if (!Flashsocket.loaded) {
120 if (typeof WEB_SOCKET_SWF_LOCATION === 'undefined') {
121 // Set the correct file based on the XDomain settings
122 WEB_SOCKET_SWF_LOCATION = path.join('/');
126 WebSocket.loadFlashPolicyFile('xmlsocket://' + options.host + ':' + port);
129 WebSocket.__initialize();
130 Flashsocket.loaded = true;
137 if (document.body) return init();
143 * Check if the FlashSocket transport is supported as it requires that the Adobe
144 * Flash Player plug-in version `10.0.0` or greater is installed. And also check if
145 * the polyfill is correctly loaded.
151 Flashsocket.check = function () {
153 typeof WebSocket == 'undefined'
154 || !('__initialize' in WebSocket) || !swfobject
157 return swfobject.getFlashPlayerVersion().major >= 10;
161 * Check if the FlashSocket transport can be used as cross domain / cross origin
162 * transport. Because we can't see which type (secure or insecure) of .swf is used
163 * we will just return true.
169 Flashsocket.xdomainCheck = function () {
174 * Disable AUTO_INITIALIZATION
177 if (typeof window != 'undefined') {
178 WEB_SOCKET_DISABLE_AUTO_INITIALIZATION = true;
182 * Add the transport to your public io.transports array.
187 io.transports.push('flashsocket');
189 'undefined' != typeof io ? io.Transport : module.exports
190 , 'undefined' != typeof io ? io : module.parent.exports