# ActionScript code to attempt to connect over rtmp and if that fails rtmpt (rollover)

**URL:** <https://community.wowza.com/t/actionscript-code-to-attempt-to-connect-over-rtmp-and-if-that-fails-rtmpt-rollover/37>\
**Category:** Wowza Developer Dojo\
**Tags:** wowza-streaming-server-java-api\
**Created:** [November 7, 2007, 7:07am UTC](https://community.wowza.com/t/actionscript-code-to-attempt-to-connect-over-rtmp-and-if-that-fails-rtmpt-rollover/37 "2007-11-07T07:07:13Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/community.wowza.com/system/32/447_2.png) [@system](https://community.wowza.com/u/system)\
**Post date:** [November 7, 2007, 7:07am UTC](https://community.wowza.com/t/actionscript-code-to-attempt-to-connect-over-rtmp-and-if-that-fails-rtmpt-rollover/37/1 "2007-11-07T07:07:13Z")

</div>

[https://www.wowza.com/docs/how-to-set-up-protocol-rollover-with-actionscript](https://www.wowza.com/docs/how-to-set-up-protocol-rollover-with-actionscript)

---

<div class="post-metadata">

**Author:** ![Charlie\_Good](https://sea2.discourse-cdn.com/flex002/user_avatar/community.wowza.com/charlie_good/32/383_2.png) [@Charlie\_Good](https://community.wowza.com/u/Charlie_Good)\
**Post date:** [February 25, 2010, 4:04am UTC](https://community.wowza.com/t/actionscript-code-to-attempt-to-connect-over-rtmp-and-if-that-fails-rtmpt-rollover/37/2 "2010-02-25T04:04:20Z")

</div>

There really is not any magic here. It should just work every where. What aspect is not working for you?

Charlie

---

<div class="post-metadata">

**Author:** ![Charlie\_Good](https://sea2.discourse-cdn.com/flex002/user_avatar/community.wowza.com/charlie_good/32/383_2.png) [@Charlie\_Good](https://community.wowza.com/u/Charlie_Good)\
**Post date:** [February 25, 2010, 9:19am UTC](https://community.wowza.com/t/actionscript-code-to-attempt-to-connect-over-rtmp-and-if-that-fails-rtmpt-rollover/37/3 "2010-02-25T09:19:07Z")

</div>

I do not know of anything to look at specifically. If you have get it down to a reproducible case, then I can look at it.

Charlie

---

<div class="post-metadata">

**Author:** ![Mike\_Donofrio](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@Mike\_Donofrio](https://community.wowza.com/u/Mike_Donofrio)\
**Post date:** [May 3, 2010, 5:44pm UTC](https://community.wowza.com/t/actionscript-code-to-attempt-to-connect-over-rtmp-and-if-that-fails-rtmpt-rollover/37/4 "2010-05-03T17:44:44Z")

</div>

I got this rollover to work nicely on my laptop for development, using Windows XP. Thanks for the code for that.

Then I tested it on Amazon AWS EC2 (Fedora 8) and it worked there also, but it took about 15 seconds to return onStatus == Failure. Seems like a long time to ask a client to wait. Anybody know a setting on the AWS EC2 Fedora that controls that? A connect timeout setting?

I did a Google search, but got some conflicting solutions and had concerns about changing settings with the OS.

Is there a simpler Java configuration setting I overlooked?

---

<div class="post-metadata">

**Author:** ![Joe\_Pritchard](https://avatars.discourse-cdn.com/v4/letter/j/e9bcb4/32.png) [@Joe\_Pritchard](https://community.wowza.com/u/Joe_Pritchard)\
**Post date:** [January 27, 2010, 6:56am UTC](https://community.wowza.com/t/actionscript-code-to-attempt-to-connect-over-rtmp-and-if-that-fails-rtmpt-rollover/37/5 "2010-01-27T06:56:46Z")

</div>

Hi Charlie,

We implemented this into our wowza player and most of the time it works well. Unfortunately there have been some random instances of our video beginning to play and then being interupted after say 5 seconds with the error message we set to display at your “Failed to connect after “+ncTryIndex+” tries”);" point.

As mentioned it is only happening to a selection of people. We are not sure if it may be a strain on the server perhaps? That somehow sends a failed status when it has actually already connected?

We are using the code like below and replaced nc.connect(serverName) in the doConnect() function in your standard as2 player with tryConnect();

We set init() to run just before mainInit() at the bottom of the standard player.

As mentioned most of the time it works fine and it definitely switches ports if the first one is blocked. Any ideas? Thanks

var ncConnectTimer:Number = 0;

var ncConnectTimeout:Number = 8000; // 8 seconds

var ncDomain:String = “OUR IP”;

var ncProtocol:String = “rtmp”;

var ncProtocol2:String = “rtmpt”;

var ncPort:Number = 1935;

var ncPort2:Number = 80;

var ncApplication:String = “flvsecure”;

var ncTryIndex:Number = 0;

var url:String = “”;

function tryConnect()

{

clearConnectTimer();

switch(ncTryIndex)

{

case 0:

ncConnectTimeout= 8000

url = ncProtocol + “://”+ncDomain+":"+ncPort+"/"+ncApplication;

trace("tried " + url)

break;

case 1:

ncConnectTimeout= 15000

ncPort = ncPort2;

ncProtocol = ncProtocol2;

url = ncProtocol + “://”+ncDomain+":"+ncPort+"/"+ncApplication;

trace("tried " + url)

break;

default:

{trace(“Failed to connect after “+ncTryIndex+” tries”);

url = “”;

statusBox.text=“failed to connect”;

nc.close();

nc=null;

gotoAndStop(2);

}

break;

}

if (url.length \> 0)

{

nc.connect(url);

ncConnectTimer = setInterval(\_root, “tryReconnect”, ncConnectTimeout);

}

}

---

<div class="post-metadata">

**Author:** ![Charlie\_Good](https://sea2.discourse-cdn.com/flex002/user_avatar/community.wowza.com/charlie_good/32/383_2.png) [@Charlie\_Good](https://community.wowza.com/u/Charlie_Good)\
**Post date:** [February 25, 2010, 5:50am UTC](https://community.wowza.com/t/actionscript-code-to-attempt-to-connect-over-rtmp-and-if-that-fails-rtmpt-rollover/37/6 "2010-02-25T05:50:20Z")

</div>

I don’t think it has anything to do with this code. It sounds like the player can’t connect to Wowza. Does it happen to same people over and over again or can it happen to any one at any time?

Charlie

---

<div class="post-metadata">

**Author:** ![Richard\_Lanham](https://avatars.discourse-cdn.com/v4/letter/r/c68b51/32.png) [@Richard\_Lanham](https://community.wowza.com/u/Richard_Lanham)\
**Post date:** [January 27, 2010, 3:48pm UTC](https://community.wowza.com/t/actionscript-code-to-attempt-to-connect-over-rtmp-and-if-that-fails-rtmpt-rollover/37/7 "2010-01-27T15:48:24Z")

</div>

I think if you put a port in your initial rtmp connection string this won’t happen, i.e., make your first attempt with something like this:

rtmp://[wowza-address]:1935/yourApp

Because Flash player 10 has a built-in protocol rollover, but it is only active if you leave off the port.

Richad

---

<div class="post-metadata">

**Author:** ![Richard\_Lanham](https://avatars.discourse-cdn.com/v4/letter/r/c68b51/32.png) [@Richard\_Lanham](https://community.wowza.com/u/Richard_Lanham)\
**Post date:** [January 27, 2010, 4:06pm UTC](https://community.wowza.com/t/actionscript-code-to-attempt-to-connect-over-rtmp-and-if-that-fails-rtmpt-rollover/37/8 "2010-01-27T16:06:06Z")

</div>

In that case, make sure the process is stopped when you get “NetConnection.Connect.Succcess” notification in your NetConnection NetStatusEvent handler.

Richard

---

<div class="post-metadata">

**Author:** ![Richard\_Lanham](https://avatars.discourse-cdn.com/v4/letter/r/c68b51/32.png) [@Richard\_Lanham](https://community.wowza.com/u/Richard_Lanham)\
**Post date:** [January 27, 2010, 4:16pm UTC](https://community.wowza.com/t/actionscript-code-to-attempt-to-connect-over-rtmp-and-if-that-fails-rtmpt-rollover/37/9 "2010-01-27T16:16:47Z")

</div>

Try adding a boolean: var isConnected:Boolean = false, then set to true on connect success, and test in tryReconnect:

```auto
function tryReconnect()
{
	trace("tryReconnect");
	clearConnectTimer();
	if (isConnected)
		return;
	ncTryIndex++;
	tryConnect();
}

```

Richard

---

<div class="post-metadata">

**Author:** ![Richard\_Lanham](https://avatars.discourse-cdn.com/v4/letter/r/c68b51/32.png) [@Richard\_Lanham](https://community.wowza.com/u/Richard_Lanham)\
**Post date:** [February 25, 2010, 9:57am UTC](https://community.wowza.com/t/actionscript-code-to-attempt-to-connect-over-rtmp-and-if-that-fails-rtmpt-rollover/37/10 "2010-02-25T09:57:54Z")

</div>

I’m not sure. The only thing I can suggest is to add many trace statements to see what’s happening. (you can’t use break points because a timer is involved so you don’t want to stop the code)

Richard

---

<div class="post-metadata">

**Author:** ![Richard\_Lanham](https://avatars.discourse-cdn.com/v4/letter/r/c68b51/32.png) [@Richard\_Lanham](https://community.wowza.com/u/Richard_Lanham)\
**Post date:** [May 3, 2010, 6:26pm UTC](https://community.wowza.com/t/actionscript-code-to-attempt-to-connect-over-rtmp-and-if-that-fails-rtmpt-rollover/37/11 "2010-05-03T18:26:25Z")

</div>

The client-side of this is using a timer that is set for 3000 milliseconde (3 seconds). If the server returns “NetConnection.Connect.Failed” notification before that, a rollover happens, but otherwise 3 seconds is the interval. So check your code. Should be 3 seconds, and you can set it for less if you want.

Richard

---

<div class="post-metadata">

**Author:** ![Richard\_Lanham](https://avatars.discourse-cdn.com/v4/letter/r/c68b51/32.png) [@Richard\_Lanham](https://community.wowza.com/u/Richard_Lanham)\
**Post date:** [May 7, 2010, 5:49pm UTC](https://community.wowza.com/t/actionscript-code-to-attempt-to-connect-over-rtmp-and-if-that-fails-rtmpt-rollover/37/12 "2010-05-07T17:49:34Z")

</div>

Jake Hilton has a port tester here:

[http://www.jakehilton.com/?q=node/43](http://www.jakehilton.com/?q=node/43)

You can use the one running on that page by putting your Wowza IP address and an Appname in the text boxes, and there is downloads for AS2 and Flex versions.

Richard

---

<div class="post-metadata">

**Author:** ![Richard\_Lanham](https://avatars.discourse-cdn.com/v4/letter/r/c68b51/32.png) [@Richard\_Lanham](https://community.wowza.com/u/Richard_Lanham)\
**Post date:** [June 19, 2010, 8:37pm UTC](https://community.wowza.com/t/actionscript-code-to-attempt-to-connect-over-rtmp-and-if-that-fails-rtmpt-rollover/37/13 "2010-06-19T20:37:35Z")

</div>

Flowplayer might have a plugin for this already. But if not you will have to modify Flowplayer source code. You have to set up a development environment for Flowplayer:

[http://flowplayer.org/documentation/developer/development-environment.html](http://flowplayer.org/documentation/developer/development-environment.html)

Then use Flex or Flash Builder menu: Search \> Find in Files, and search for “.connect”

When you find where the RTMP connection happens, you can work that code in.

Richard

---

<div class="post-metadata">

**Author:** ![Sebastien\_Faure](https://avatars.discourse-cdn.com/v4/letter/s/9f8e36/32.png) [@Sebastien\_Faure](https://community.wowza.com/u/Sebastien_Faure)\
**Post date:** [June 20, 2010, 6:13pm UTC](https://community.wowza.com/t/actionscript-code-to-attempt-to-connect-over-rtmp-and-if-that-fails-rtmpt-rollover/37/14 "2010-06-20T18:13:44Z")

</div>

Flowplayer RTMP plugin can natively handle RTMP/RTMPT/RTMPE/RMPTE rollover. See plugin code [here](http://code.google.com/p/flowplayer-plugins/source/browse/flash/rtmp/trunk/src/actionscript/org/flowplayer/rtmp/RTMPConnectionProvider.as).

But there’s a weird behaviour when using RTMPE : [http://flowplayer.org/forum/5/37221](http://flowplayer.org/forum/5/37221).

Still no fix …

---

<div class="post-metadata">

**Author:** ![Joe\_Pritchard](https://avatars.discourse-cdn.com/v4/letter/j/e9bcb4/32.png) [@Joe\_Pritchard](https://community.wowza.com/u/Joe_Pritchard)\
**Post date:** [January 27, 2010, 7:52am UTC](https://community.wowza.com/t/actionscript-code-to-attempt-to-connect-over-rtmp-and-if-that-fails-rtmpt-rollover/37/15 "2010-01-27T07:52:31Z")

</div>

Hi,

Thanks for your quick response but if I understand it correctly, I am already doing this.

i.e

var ncProtocol:String = “rtmp”;

var ncProtocol2:String = “rtmpt”;

var ncPort:Number = 1935;

var ncPort2:Number = 80;

var ncApplication:String = “flvsecure”;

var ncTryIndex:Number = 0;

url = ncProtocol + “://”+ncDomain+":"+ncPort+"/"+ncApplication;

Thanks,

Joe

---

<div class="post-metadata">

**Author:** ![Joe\_Pritchard](https://avatars.discourse-cdn.com/v4/letter/j/e9bcb4/32.png) [@Joe\_Pritchard](https://community.wowza.com/u/Joe_Pritchard)\
**Post date:** [January 27, 2010, 8:04am UTC](https://community.wowza.com/t/actionscript-code-to-attempt-to-connect-over-rtmp-and-if-that-fails-rtmpt-rollover/37/16 "2010-01-27T08:04:37Z")

</div>

For reference, I am using flash player 9 when i compile the swf…

---

<div class="post-metadata">

**Author:** ![Joe\_Pritchard](https://avatars.discourse-cdn.com/v4/letter/j/e9bcb4/32.png) [@Joe\_Pritchard](https://community.wowza.com/u/Joe_Pritchard)\
**Post date:** [January 27, 2010, 8:11am UTC](https://community.wowza.com/t/actionscript-code-to-attempt-to-connect-over-rtmp-and-if-that-fails-rtmpt-rollover/37/17 "2010-01-27T08:11:43Z")

</div>

Thanks, I have used

if(infoObj.code == “NetConnection.Connect.Success”) clearConnectTimer();

with the same clearConnectTimer function from this post, is this what you mean? When I test by closing the ports on my firewall it works ask expected and works on our live server 99% of the time. It’s just some cases that video plays for a few seconds and then brings up the error message.

Thanks,

Joe

---

<div class="post-metadata">

**Author:** ![Joe\_Pritchard](https://avatars.discourse-cdn.com/v4/letter/j/e9bcb4/32.png) [@Joe\_Pritchard](https://community.wowza.com/u/Joe_Pritchard)\
**Post date:** [January 27, 2010, 8:27am UTC](https://community.wowza.com/t/actionscript-code-to-attempt-to-connect-over-rtmp-and-if-that-fails-rtmpt-rollover/37/18 "2010-01-27T08:27:51Z")

</div>

Hi Richard, Thanks for this suggestion. I am going to try it. I should have really thought of this myself, as it is almost as though we are getting a NetConnection.Connect.Failed status after we have already got NetConnection.Connect.Success? If this is what’s happening, I have no idea why, but it makes sense that your change would solve this.

Nice one

---

<div class="post-metadata">

**Author:** ![Joe\_Pritchard](https://avatars.discourse-cdn.com/v4/letter/j/e9bcb4/32.png) [@Joe\_Pritchard](https://community.wowza.com/u/Joe_Pritchard)\
**Post date:** [February 25, 2010, 1:17am UTC](https://community.wowza.com/t/actionscript-code-to-attempt-to-connect-over-rtmp-and-if-that-fails-rtmpt-rollover/37/19 "2010-02-25T01:17:23Z")

</div>

Hi,

I am still having this problem, so this fix doesn’t seem to have sorted it.

It seems that the problem is happening in Firefox, I have not yet had a report of it happening in IE and cannot recreate it in IE.

I am using the as2 simple player example with this mod as I have detailed.

I am using the simple player in an external swf and loading it into my main swf everytime I start a new video, simply passing the file name in. Is there anything in the external swf that could be affecting it or perhaps cache of the last version of swf’s net connection info. Excuse my poor knowledge if this sounds stupid!

Thanks, this problem is really difficult to dianose as it is intermittent but I am pretty sure it is Firefox specific, unless it is Flash Player specific?

Thanks,

Joe

---

<div class="post-metadata">

**Author:** ![Joe\_Pritchard](https://avatars.discourse-cdn.com/v4/letter/j/e9bcb4/32.png) [@Joe\_Pritchard](https://community.wowza.com/u/Joe_Pritchard)\
**Post date:** [February 25, 2010, 2:09am UTC](https://community.wowza.com/t/actionscript-code-to-attempt-to-connect-over-rtmp-and-if-that-fails-rtmpt-rollover/37/20 "2010-02-25T02:09:11Z")

</div>

Thanks, one of the reasons I haven’t been able to do it this way is that it only seems to be happening on our production servers, I’m not sure if this is useful information. I have flash player debugger on IE so that I can get traces from the live swf, I’ll set this up for Firefox and see if I can see anything more. This was just my first steps incase there were any known issues with certain versions of firefox/certain versions of flash player! thanks

[Next page](https://community.wowza.com/t/actionscript-code-to-attempt-to-connect-over-rtmp-and-if-that-fails-rtmpt-rollover/37.md?page=2)
