Why IE doesn’t drop Flash NetConnections / NetStreams and how to fix it.

Problem

The Adobe Flash Player ActiveX control for Internet Explorer 7 (I don’t know about IE6) will not disconnect NetConnections if the connection was made by a swf instance loaded in a tab and only that tab was closed. If IE itself is closed though the NetConnection will be dropped. This could lead to hung “ghost” connections where you won’t hear any audio, etc. but your Wowza Media Server (and maybe Flash Media Server, who knows?) will still think the client is connected.

Explanation

Laziness on the part of two large corporations.

Solution

Register a JS handler function for window.onbeforeunload that uses the Flash ExternalInterface to call a function in your swf that manually closes the NetConnection. The event “onbeforeunload” is recognized and executed by IE, Safari and Firefox before the onunload event is fired. It is apparently not handled by Opera AFAIK but that’s ok since IE is the only one that actually needs this fix.

In your ActionScript 3 code somewhere:


import flash.external.ExternalInterface;

....

private someFunction():void{

if( ExternalInterface.available ){

ExternalInterface.addCallback("disconnect", disconnect);

}

}

private function disconnect():void {

_my_net_connection1.close();

}

In your Javascript code for the page that loads your swf:


window.onbeforeunload = function(){

// pure JS

var swf = document.getElementById('mySwf');

swf.disconnect();

// jQuery 1.2.6 version

$("#mySwf")[0].disconnect();

}

The End

(image originally from visualizeus)

Tags: , , , , ,

2 Responses to “Why IE doesn’t drop Flash NetConnections / NetStreams and how to fix it.”

  1. baklach Says:

    ok!!!

    my Flash function passes variables to PHP which removes XML nods in XML file. It is kinde online user list for flash chat. In Flash it works great wen Log Out button pressed!!!! So when i call the function from Javascript it works but only onLoad, not “onbeforeunload”. it seems that flash can’t pass the data to PHP befor browser window closed!!!!! this is making me crazy!!!!! i cant figure it out!!!! Could You please Help me!!!!!!!!!!!!!!???????

    Thanks a lot!!!!!!!!!!!!!!!

  2. fix blue screen Says:

    Thanks for sharing this excellent post, i truly love your weblog, but i’ve some problem i dont know whether it’s my side problem or in your site? some words of the submit on your blog have charactor encoding problem, yes i use auto detecting, can u pls look into this problem a bit?

Leave a Reply