Flex3部分
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.events.CloseEvent;
import mx.collections.ArrayCollection;
import mx.controls.Alert;
private var listSO:SharedObject;
private var userArr:Array;
private var conn:NetConnection;
private var localUsername:String;
private var stm:NetStream;
private var stm2:NetStream;
//摄像头
private var cam:Camera;
//麦克风
private var mic:Microphone;
private var video_0:Video;
private var video_1:Video;
[Bindable]
public var cards:ArrayCollection;
//视频邀请集合
public var videoUsers:Array;
protected function login(event:MouseEvent):void
{
// TODO Auto-generated method stub
localUsername= txt_name.text;
if(localUsername==""){
Alert.show("用户名不能为空");
return;
}else{
if(conn==null){
conn = new NetConnection();
conn.client = this;
conn.addEventListener(NetStatusEvent.NET_STATUS, _statusHandler);
conn.connect("rtmp://192.168.0.10/szftest", localUsername);
}
}
}
//状态监听
private function _statusHandler(evt:NetStatusEvent):void {
if (evt.info.code == "NetConnection.Connect.Success") {
this.currentState = "chat";
Alert.show("连接成功");
_setListSO(); //创建用户列表共享对象
this.showJoinInInfo(localUsername);
}
if (evt.info.code == "NetConnection.Connect.Failed") {
Alert.show("连接失败");
}
if (evt.info.code == "NetConnection.Connect.Closed") {
Alert.show("连接关闭");
}
}
public function showJoinInInfo(message:String) : void
{
//conn.call("getOnloadUser", new Responder(result_getUsers);
conn.call("getOnloadUser", null,message);
}
public function result_getOnloadUser(str:String):void {
txt_chatmsg.text += str + "加入聊天室" + "\n";
}
//创建用户列表共享对象
private function _setListSO():void{
listSO = SharedObject.getRemote("listSO", conn.uri, false);
listSO.connect(conn);
listSO.addEventListener(SyncEvent.SYNC,_listSOSyncHandler);
}
//用户列表共享对象被更新之后事件
private function _listSOSyncHandler(evt:SyncEvent):void{
_showUserList(); //更新用户列表
}
//更新用户列表
private function _showUserList():void{
cards = new ArrayCollection(
[ {label:"All"}]
);
userArr = new Array();
//用户数组更新
for (var tmp:String in listSO.data) {
userArr.push(listSO.data[tmp]);
}
//添加ArrayCollection
for (var i:int = 0; i < userArr.length; i++ ) {
cards.addItem( { label:userArr[i] } );
}
//将数组添加到列表中显示出来
userList.dataProvider = cards;
users.dataProvider=cards;
}
public function showMessage(message:String) : void
{
txt_chatmsg.text += message + "\n";
}
protected function sendMessage(event:MouseEvent):void
{
// TODO Auto-generated method stub
var sendString:String = txt_yousay.text;
//var sendTo:String=users.selectedItem.userName==null?"":users.selectedItem.userId;
var sendTo:String=userList.selectedItem.label;
txt_yousay.text = "";
conn.call("sayToAll", null,sendTo,sendString);
}
//断线通知
public function disconnectMessage(disUser:String):void{
txt_chatmsg.text += disUser + "退出聊天室\n";
}
//视频邀请
protected function video_clickHandler(event:MouseEvent):void
{
// TODO Auto-generated method stub
var sendTo:String=userList.selectedItem.label;
conn.call("videoInvite", null,sendTo);
}
//显示自己的视频
public function _publishVideo():void{
stm = new NetStream(conn);
cam = Camera.getCamera();
if(cam==null)
{
Alert.show("没有可以使用的摄像头");
return;
}else{
cam.setLoopback(true);
cam.setMotionLevel(50,100);
cam.setMode(320,240,15,true);
stm.attachCamera(cam);
}
//获取麦克风
mic = Microphone.getMicrophone();
if(mic==null){
Alert.show("没有可以使用的麦克风");
}else{
mic.setUseEchoSuppression(true);
stm.attachAudio(mic);
}
stm.publish("somes"+txt_name.text,"live");
video_1 = new Video();
video_1.width = my_video.width;
video_1.height = my_video.height;
video_1.attachCamera(cam);
my_video.addChild(video_1);
}
//被邀请方获取对方视频
private function _getVideo():void{
stm2 = new NetStream(conn);
video_0 = new Video(other_video.width,other_video.height);
video_0.attachNetStream(stm2);
other_video.addChild(video_0);
stm2.play("somes"+videoUsers[0]);
}
//邀请方获取对方视频
private function _getOtherVideo(other:String):void {
stm2 = new NetStream(conn);
video_0 = new Video(other_video.width,other_video.height);
video_0.attachNetStream(stm2);
other_video.addChild(video_0);
stm2.play("somes"+other);
}
//视频被邀请方调用
public function showInviteMessage(message:String):void{
videoUsers=message.split(";");
Alert.show(videoUsers[0]+"邀请你视频,是否同意?","是吗",Alert.YES|Alert.NO,this,alertClientHandel);
}
//被邀请方同意调用
public function alertClientHandel(event:CloseEvent):void{
if(event.detail==Alert.YES){
_publishVideo();
_getVideo();
conn.call("agreeVideoInvite",null,videoUsers[0],videoUsers[1]);
}else{
}
}
//邀请方接到被邀请方的同意后调用
public function showVideo(other:String):void{
_publishVideo();
_getOtherVideo(other);
}
private function closeStream():void{
Alert.show("关闭");
}
]]>
</mx:Script>
<mx:states>
<mx:State name="chat">
<mx:SetProperty target="{form1}" name="width" value="0"/>
<mx:SetProperty target="{form1}" name="height" value="0"/>
<mx:SetProperty target="{form1}" name="x" value="0"/>
<mx:SetProperty target="{form1}" name="y" value="0"/>
<mx:AddChild position="lastChild">
<mx:Panel x="10" y="10" width="381" height="370" layout="absolute" title="聊天信息">
<mx:TextArea x="10" y="10" width="215" height="235" id="txt_chatmsg"/>
<mx:ComboBox x="233" y="34" width="118" id="userList"></mx:ComboBox>
<mx:Label x="233" y="11" text="用户列表"/>
<mx:DataGrid x="233" y="64" height="256" id="users" width="118">
<mx:columns>
<mx:DataGridColumn headerText="用户名" dataField="label"/>
</mx:columns>
</mx:DataGrid>
<mx:TextInput x="10" y="253" height="67" width="150" id="txt_yousay"/>
<mx:Button x="168" y="253" label="发送" width="57" click="sendMessage(event)"/>
<mx:Button x="168" y="253" label="发送" width="57" click="sendMessage(event)"/>
<mx:Button x="168" y="298" label="视频" width="57" click="video_clickHandler(event)"/>
</mx:Panel>
</mx:AddChild>
<mx:AddChild position="lastChild">
<mx:VideoDisplay x="399" y="30" width="181" height="158" id="my_video"/>
</mx:AddChild>
<mx:AddChild position="lastChild">
<mx:Label x="399" y="10" text="我的"/>
</mx:AddChild>
<mx:AddChild position="lastChild">
<mx:VideoDisplay x="399" y="224" width="181" height="156" id="other_video"/>
</mx:AddChild>
<mx:AddChild position="lastChild">
<mx:Label x="399" y="198" text="对方的"/>
</mx:AddChild>
</mx:State>
</mx:states>
<mx:Form x="10" y="10" width="283" height="126" id="form1">
<mx:FormItem label="用户名:">
<mx:TextInput id="txt_name"/>
</mx:FormItem>
<mx:FormItem>
<mx:Button label="登陆" click="login(event)"/>
</mx:FormItem>
</mx:Form>
</mx:WindowedApplication>