Skip to content

Instantly share code, notes, and snippets.

@neodigm
Created May 9, 2017 19:52
Show Gist options
  • Save neodigm/203e36b55c75da6ea6bbf65be4fcd385 to your computer and use it in GitHub Desktop.
Save neodigm/203e36b55c75da6ea6bbf65be4fcd385 to your computer and use it in GitHub Desktop.
Flash / Flex intelligent port location and protocol tunneling
/*
This ActionScript package will except a string of port numbers in most
likely order (from the configuration XML).
For example: 1935, 8080, 80.
It will then attempt to connect to each port in sequence
every 800 milliseconds. If port 80 is used then the
tunneling protocol is returned (rtmpt).
*/
/* Create the NEOS namespace
*/
if (null == _global.NEOS) {
_global.NEOS = new Object();
}
/* Constructor: FailSafeConn wraps the NetConnection object(s)
*/
function FailSafeConn(spPort){
this._fsc_nc = new NetConnection();
this._fsc_nc._port = spPort;
}
/* Called by cron (setInterval). Executes the NetConnection connection(s)
*/
FailSafeConn.prototype.connect = function(iIDX, spURI){
clearInterval(_global.NEOS.videoConn.aC[iIDX][5]);
this._fsc_nc.onStatus = function(info){
if (! _global.NEOS.videoConn.valid_port){
if ("NetConnection.Connect.Success" == info.code) {
_global.NEOS.videoConn.valid_port = true;
_global.NEOS.videoConn.statusMsg = "Valid Connection: port# " + this._port;
_global.NEOS.videoConn.foundValid(this); // callback
}else{
_global.NEOS.videoConn.statusMsg = "Invalid Connection: port# " + this._port;
this.close();
}
}
}
if (_global.NEOS.videoConn.valid_port){
this._fsc_nc.close();
}else{
this._fsc_nc.connect(spURI);
}
}
/* Constructor: FailSafe / Requires a comma delim list of ports in most likely order (from XML)
*/
function FailSafe(spPorts) {
this.statusMsg = "Not Connected";
this.valid_port = false;
this.Ports = spPorts;
this.aC = this.Ports.split(",");
}
/* Creates and schedules all of the connections.
*/
FailSafe.prototype.connect = function(spDomain, spApplication, spInstance){
var iCron = 100;
var aE = new Array();
this.statusMsg = "Creating Connections ...";
for (var i = 0; i<this.aC.length; i++){
aE[0] = this.aC[i];
aE[1] = new FailSafeConn(aE[0]);
if ("1935" == aE[0]) {
aE[2] = "rtmp";
aE[3] = spDomain;
}else{
aE[2] = "rtmpt";
aE[3] = spDomain + ":" + aE[0];
}
aE[4] = aE[2] + "://" + aE[3] + "/" + spApplication + "/" + spInstance;
aE[5] = setInterval(this.cron, iCron, i);
this.aC[i] = [aE[0], aE[1], aE[2], aE[3], aE[4], aE[5]];
iCron = iCron + 800;
}
}
FailSafe.prototype.cron = function(iIDX){
if (! _global.NEOS.videoConn.valid_port){
_global.NEOS.videoConn.statusMsg = "Attempting: port# " + _global.NEOS.videoConn.aC[iIDX][0];
}
_global.NEOS.videoConn.aC[iIDX][1].connect(iIDX, _global.NEOS.videoConn.aC[iIDX][4]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment