Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ function Socket (opts) {
this.transports = opts.transports || ['polling', 'websocket', 'flashsocket'];
this.readyState = '';
this.writeBuffer = [];
this.policyPort = opts.policyPort;
this.open();
};

Expand Down Expand Up @@ -78,6 +79,7 @@ Socket.prototype.createTransport = function (name) {
, query: query
, forceJSONP: this.forceJSONP
, flashPath: this.flashPath
, policyPort: this.policyPort
});

return transport;
Expand Down
94 changes: 80 additions & 14 deletions lib/transports/flashsocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Module dependencies.
*/

var WebSocket = require('./websocket')
var WS = require('./websocket')
, util = require('../util')

/**
Expand All @@ -19,15 +19,16 @@ module.exports = FlashWS;
*/

function FlashWS (options) {
WebSocket.call(this, options);
WS.call(this, options);
this.flashPath = options.flashPath;
this.policyPort = options.policyPort;
};

/**
* Inherits from WebSocket.
*/

util.inherits(FlashWS, WebSocket);
util.inherits(FlashWS, WS);

/**
* Transport name.
Expand All @@ -44,7 +45,7 @@ FlashWS.prototype.name = 'flashsocket';
*/

FlashWS.prototype.doOpen = function () {
if (!check()) {
if (!this.check()) {
// let the probe timeout
return;
}
Expand All @@ -59,16 +60,23 @@ FlashWS.prototype.doOpen = function () {

WEB_SOCKET_LOGGER = { log: log('debug'), error: log('error') };
WEB_SOCKET_SUPPRESS_CROSS_DOMAIN_SWF_ERROR = true;
WEB_SOCKET_DISABLE_AUTO_INITIALIZATION = true;

// dependencies
var deps = [path + 'web_socket.js'];
var deps = [this.flashPath + 'web_socket.js'];

if ('undefined' == typeof swfobject) {
deps.unshift(path + 'swfobject.js');
deps.unshift(this.flashPath + 'swfobject.js');
}

var self = this;

load(deps, function () {
FlashWS.prototype.doOpen.call(self);
self.ready(function () {
WebSocket.__addTask(function () {
WS.prototype.doOpen.call(self);
});
});
});
};

Expand All @@ -80,24 +88,82 @@ FlashWS.prototype.doOpen = function () {

FlashWS.prototype.doClose = function () {
if (!this.socket) return;
FlashWS.prototype.doClose.call(this);
var self = this;
WebSocket.__addTask(function() {
WS.prototype.doClose.call(self);
});
};

FlashWS.prototype.write = function() {
var self = this, args = arguments;
WebSocket.__addTask(function () {
WS.prototype.write.apply(self, args);
});
};

FlashWS.prototype.ready = function (fn) {
if (typeof WebSocket == 'undefined'
|| !('__initialize' in WebSocket) || !swfobject
) {
return;
}

if (swfobject.getFlashPlayerVersion().major < 10) {
return;
}

function init () {

// Only start downloading the swf file when the checked that this browser
// actually supports it
if (!FlashWS.loaded) {

if (self.policyPort !== 843) {
WebSocket.loadFlashPolicyFile('xmlsocket://' + self.host + ':' + self.policyPort);
}

WebSocket.__initialize();
FlashWS.loaded = true;
}

fn.call(self);
}

var self = this;
if (document.body) return init();

util.load(init);
};

/**
* Feature detection for FlashSocket.
* Feature detection for flashsocket.
*
* @return {Boolean} whether this transport is available.
* @api public
*/

function check () {
FlashWS.prototype.check = function () {
// if node
return false;
// end

for (var i = 0, l = navigator.plugins.length; i < l; i++) {
for (var j = 0, m = navigator.plugins[i].length; j < m; j++) {
if (navigator.plugins[i][j] == 'Shockwave Flash') return true;
if (typeof WebSocket != 'undefined' && !('__initialize' in WebSocket))
return false;

if (window.ActiveXObject) {
var control = null;
try {
control = new ActiveXObject('ShockwaveFlash.ShockwaveFlash');
} catch (e) { }
if (control) {
return true;
}
}
else {
for (var i = 0, l = navigator.plugins.length; i < l; i++) {
for (var j = 0, m = navigator.plugins[i].length; j < m; j++) {
if (navigator.plugins[i][j].description == 'Shockwave Flash') return true;
}
}
}

Expand Down Expand Up @@ -156,7 +222,7 @@ function load (arr, fn) {
function process (i) {
if (!arr[i]) return fn();
create(arr[i], function () {
process(arr[++i]);
process(++i);
});
};

Expand Down
29 changes: 16 additions & 13 deletions lib/transports/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ WS.prototype.name = 'websocket';
*/

WS.prototype.doOpen = function () {
if (!check()) {
if (!this.check()) {
// let probe timeout
return;
}
Expand Down Expand Up @@ -88,7 +88,9 @@ WS.prototype.write = function (packets) {
*/

WS.prototype.doClose = function () {
this.socket.close();
if (typeof this.socket !== 'undefined') {
this.socket.close();
}
};

/**
Expand Down Expand Up @@ -116,6 +118,18 @@ WS.prototype.uri = function () {
return schema + '://' + this.host + port + this.path + query;
};

/**
* Feature detection for WebSocket.
*
* @return {Boolean} whether this transport is available.
* @api public
*/

WS.prototype.check = function () {
var websocket = ws();
return !!websocket && !('__initialize' in websocket && this.name === WS.prototype.name);
}

/**
* Getter for WS constructor.
*
Expand All @@ -129,14 +143,3 @@ function ws () {

return global.WebSocket || global.MozWebSocket;
}

/**
* Feature detection for WebSocket.
*
* @return {Boolean} whether this transport is available.
* @api public
*/

function check () {
return !!ws();
}