I don't know if anyone remembers the old Yahoo chat rooms, but I currently have an existing chat on my website. It is Ajax Chat by blueimp. All is well and it works just fine. I am wanting to add a "talk" button so that a user can press it and talk and everyone else in the chat room can hear. What I have works sort of. But after a few minutes of use not all users can hear the person talking. So my question is, how can I fix this so that users can all continue to hear whoever is talking?
Here is the code:
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.events.*;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLVariables;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequestMethod;
import flash.media.Microphone;
import flash.media.Sound;
var Mic:Microphone;
var aud:Sound;
var nc:NetConnection = new NetConnection;
nc.client = this;
var istream:NetStream;
var ostream:NetStream;
Security.showSettings("2");
if(Microphone.names.length <= 0) {
//they has no mic
} else {
Mic = Microphone.getMicrophone();
Mic.setUseEchoSuppression(true);
Mic.setLoopBack(false);
Mic.addEventListener(ActivityEvent.ACTIVITY, activityHandler);
Mic.addEventListener(StatusEvent.STATUS, statusHandler);
}
btnTalk.addEventListener(MouseEvent.MOUSE_DOWN, talkDown);
btnTalk.addEventListener(MouseEvent.MOUSE_UP, talkUp);
nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
nc.connect("rtmp://MYSERVER:1935/oflaDemo");
function talkDown(e:MouseEvent):void {
trace("talking");
ostream.publish("charchar");
istream.receiveAudio(false);
}
function talkUp(e:MouseEvent):void {
trace("done talking");
ostream.close();
istream.receiveAudio(true);
}
function netStatusHandler(event:NetStatusEvent):void {
trace(event.info.code)
ostream = new NetStream(nc);
istream = new NetStream(nc);
istream.play("charchar");
ostream.attachAudio(Mic);
}
function activityHandler(event:ActivityEvent):void {
trace("activityHandler: " + event);
}
function statusHandler(event:StatusEvent):void {
trace("statusHandler: " + event);
}
Example: 3 users load the page and can all hear. After a few times of someone talking, User1 can hear User2, but User3 cannot hear User1. It is mixed and matched who can hear who, but this is an example of what's happening.
If you're going to use the same stream "charchar" for all clients, you will need to implement some sort of lock to prevent a race condition. That will be incredibly tricky btw and will most certainly require server side modification of the code.