My application allows users to video chat by using Flash’s P2P support. Nothing fancy, but I do wish it could be a little more dependable…
Suppose we have Client A and Client B who wish to talk to each other. My server will send a “connect” message to both clients with the other’s publishID. A will then call “startPlaying” (in the code below) with B’s publishID, and B will do the same with A’s publishID.
This works BEAUTIFULLY 95% of the time. However sometimes, although the Flash object’s “startPlaying” function will be called with the correct publishID, it won’t actually be able to connect… I don’t receive a “NetStream.Publish.Start” message. It just fails silently.
PLEASE… OMG PLEASE help me figure this out. This has consumed way too much of my life and I’m considering abandoning my project because of it. Why does the NetStream object sometimes fail to connect?
Even advice on how to find the problem would be EXTRAORDINARILY helpful.
For reference, my code looks like this:
package MyFlashLibs {
public class MyConn {
import MyFlashLibs.Constants;
import flash.media.Camera;
import flash.media.Microphone;
import MyFlashLibs.IncVid;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.events.NetStatusEvent;
import flash.external.ExternalInterface;
//======================================================================//
// CONSTRUCTOR //
//======================================================================//
public function MyConn(c:Camera, m:Microphone, iVid:IncVid)
{
cam = c;
mic = m;
incVid = iVid;
conn = new NetConnection();
conn.connect(Constants.P2P_SERVER_NAME);
conn.addEventListener(NetStatusEvent.NET_STATUS, connStatusHandler);
}
private function connStatusHandler(connectionInfo:NetStatusEvent):void
{
if (connectionInfo.info.code == "NetConnection.Connect.Success") {
pubID = conn.nearID;
startPublishing();
}
}
private function startPublishing():void
{
pubStream = new NetStream(conn, NetStream.DIRECT_CONNECTIONS);
pubStream.addEventListener(NetStatusEvent.NET_STATUS, pubStatusHandler);
pubStream.attachCamera(cam);
pubStream.attachAudio(mic);
var publishStreamClient:Object = new Object();
publishStreamClient.onPeerConnect = function(callerNS:NetStream):Boolean {
return true;
};
pubStream.client = publishStreamClient;
pubStream.publish("vid");
}
private function pubStatusHandler(event:NetStatusEvent):void
{
if (event.info.code == "NetStream.Publish.Start") {
publishing = true;
}
}
public function startPlaying(playID:String):void
{
if (!publishing)
return;
if (playStream)
playStream.close();
playStream = new NetStream(conn, playID);
playStream.addEventListener(NetStatusEvent.NET_STATUS, playStatusHandler);
playStream.client = this;
incVid.getVideo().attachNetStream(playStream);
playStream.receiveVideo(true);
playStream.receiveAudio(true);
playStream.play("vid");
}
public function stopPlaying():void
{
if (playStream)
playStream.close();
}
private function playStatusHandler(event:NetStatusEvent):void
{
if (event.info.code == "NetStream.Play.Start") {
notifyPlaySuccess();
}
}
private function notifyPlaySuccess():void
{
ExternalInterface.call("videoPlaySuccess");
}
//======================================================================//
// VARIABLES //
//======================================================================//
private var conn:NetConnection = null;
private var pubStream:NetStream = null;
private var playStream:NetStream = null;
private var pubID:String = null;
private var publishing:Boolean = false;
private var cam:Camera = null;
private var mic:Microphone = null;
private var incVid:IncVid = null;
}
}