Red5下的room
http://blog.csdn.net/whycold/article/details/6142475
package com.test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import org.red5.server.adapter.ApplicationAdapter;
import org.red5.server.api.IClient;
import org.red5.server.api.IConnection;
import org.red5.server.api.IScope;
import org.red5.server.api.service.IServiceCapableConnection;
import org.red5.server.api.Red5;
public class RoomApplication extends ApplicationAdapter{
private static HashMap<String,IScope> scopeMap = new HashMap<String,IScope>();
private static HashMap<String,List> userMap = new HashMap<String,List>();
private static HashMap<String,String> videoMap = new HashMap<String,String>();
@Override
public boolean roomConnect(IConnection conn, Object[] arg1) {
// TODO Auto-generated method stub
String userName = arg1[0].toString();
String roomName = arg1[1].toString();
if(userMap.containsKey(roomName)){
List list = userMap.get(roomName);
list.add(userName);
}else{
List<String> list = new ArrayList<String>();
list.add(userName);
userMap.put(roomName, list);
}
conn.getClient().setAttribute("userName", userName);
return super.roomConnect(conn, arg1);
}
@Override
public void roomDisconnect(IConnection client) {
// TODO Auto-generated method stub
super.roomDisconnect(client);
}
@Override
public boolean roomJoin(IClient client, IScope s) {
// TODO Auto-generated method stub
System.out.println("room join" + s.getName());
updateUserList(s.getName());
String roomName = s.getName();
String userName = client.getAttribute("userName").toString();
sendVideo(userName,roomName);
return super.roomJoin(client, s);
}
public void sendVideo(String userName,String roomName){
if(videoMap.containsKey(roomName)){
System.out.println("我要拿视频啦");
IScope s = scopeMap.get(roomName);
System.out.println(s);
Iterator <IConnection>iterator = scope.getConnections();
while(iterator.hasNext()){
System.out.println("connection");
IConnection conn = iterator.next();
if(conn.getClient().getAttribute("userName").toString().equalsIgnoreCase(userName)){
if (conn instanceof IServiceCapableConnection)
{
//转发消息
System.out.println("conn====" + conn);
IServiceCapableConnection sc = (IServiceCapableConnection) conn;
sc.invoke("netStream", new Object[]{videoMap.get(roomName)});
}
}
}
}
}
@Override
public void roomLeave(IClient client, IScope s) {
// TODO Auto-generated method stu
String userName = client.getAttribute("userName").toString();
String roomName = s.getName();
List list = userMap.get(roomName);
list.remove(userName);
updateUserList(roomName);
if(videoMap.containsKey(roomName)){
if(videoMap.get(roomName).toString().equalsIgnoreCase(userName)){
videoMap.remove(roomName);
}
}
super.roomLeave(client, s);
}
@Override
public boolean roomStart(IScope s) {
// TODO Auto-generated method stub
System.out.println("room start" + s.getName());
scopeMap.put(s.getName(), s);
return super.roomStart(s);
}
@Override
public void roomStop(IScope s) {
// TODO Auto-generated method stub
scopeMap.remove(s.getName());
System.out.println("房间彻底关闭了");
super.roomStop(s);
}
public void sendToRoom(String msg,String roomName,String userName,String toUserName){
IScope scope = scopeMap.get(roomName);
Iterator <IConnection>iterator = scope.getConnections();
while(iterator.hasNext()){
IConnection conn = iterator.next();
String connUserName = conn.getClient().getAttribute("userName").toString();
if (conn instanceof IServiceCapableConnection)
{
IServiceCapableConnection sc = (IServiceCapableConnection) conn;
if("all".equalsIgnoreCase(toUserName)){
//转发消息
String str = userName + "说:"+msg;
sc.invoke("sendToRoom", new Object[]{str});
}else if(connUserName.equalsIgnoreCase(userName)){
String str = "你对" + toUserName + "说:"+msg;
sc.invoke("sendToRoom", new Object[]{str});
}else if(connUserName.equalsIgnoreCase(toUserName)){
String str = userName + "对你说:"+msg;
sc.invoke("sendToRoom", new Object[]{str});
}
}
}
}
public void updateUserList(String roomName){
System.out.println("send userlist==" + roomName);
List list = userMap.get(roomName);
System.out.println("有"+list.size()+"个子");
IScope s = scopeMap.get(roomName);
System.out.println(s);
Iterator <IConnection>iterator = scope.getConnections();
while(iterator.hasNext()){
System.out.println("connection");
IConnection conn = iterator.next();
if (conn instanceof IServiceCapableConnection)
{
//转发消息
System.out.println("conn====" + conn);
IServiceCapableConnection sc = (IServiceCapableConnection) conn;
sc.invoke("updateList", new Object[]{list});
}
}
}
public void playVideo(String userName,String roomName){
IScope scope = scopeMap.get(roomName);
videoMap.put(roomName, userName);
Iterator <IConnection>iterator = scope.getConnections();
while(iterator.hasNext()){
IConnection conn = iterator.next();
if (conn instanceof IServiceCapableConnection)
{
IServiceCapableConnection sc = (IServiceCapableConnection) conn;
sc.invoke("netStream", new Object[]{userName});
}
}
}
public void escVideo(String roomName){
IScope scope = scopeMap.get(roomName);
videoMap.remove(roomName);
Iterator <IConnection>iterator = scope.getConnections();
while(iterator.hasNext()){
IConnection conn = iterator.next();
if (conn instanceof IServiceCapableConnection)
{
IServiceCapableConnection sc = (IServiceCapableConnection) conn;
sc.invoke("escVideo", new Object[]{});
}
}
}
public void checkVideo(String userName,String roomName){
System.out.println("checkVideo");
IScope scope = scopeMap.get(roomName);
Iterator <IConnection>iterator = scope.getConnections();
while(iterator.hasNext()){
IConnection conn = iterator.next();
String connUserName = conn.getClient().getAttribute("userName").toString();
System.out.println("conUserName");
if(userName.equalsIgnoreCase(connUserName)){
if (conn instanceof IServiceCapableConnection)
{
IServiceCapableConnection sc = (IServiceCapableConnection) conn;
if(videoMap.containsKey(roomName)){
sc.invoke("canNotVideo", new Object[]{});
}else{
System.out.println("canvideo");
sc.invoke("canVideo", new Object[]{});
}
}
}
}
}
}
mxml:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="con()">
<mx:Script>
<![CDATA[
import mx.events.ListEvent;
import flash.utils.flash_proxy;
import mx.core.UIComponent;
import mx.messaging.AbstractConsumer;
import mx.collections.ArrayCollection;
import flash.events.*;
import flash.media.Video;
import flash.net.*;
import flash.external.ExternalInterface;
[Bindable]
private var net:NetConnection;
import mx.controls.Alert;
private var username:String;
private var roomname:String;
[Bindable]
private var arrayCon:ArrayCollection;
[Bindable]
private var comboArrayCon:ArrayCollection;
var ns:NetStream;
var nsPlay:NetStream;
public function con():void{
//chat.addEventListener(Event.ENTER_FRAME,onEnterFrame);
username = parameters.name;
roomname = parameters.roomname;
//username="22";
//roomname="11";
if(comboArrayCon==null){
comboArrayCon = new ArrayCollection();
}
comboArrayCon.addItem("all");
//Alert.show(username,roomname);
l.text = "您在"+roomname+"聊天室里!";
if(net==null){
net = new NetConnection();
net.addEventListener(NetStatusEvent.NET_STATUS,netStatus);
net.addEventListener(AsyncErrorEvent.ASYNC_ERROR,onAsyncError);
net.connect("rtmp://192.168.1.32/mychat/"+roomname+"/",username,roomname);
net.client = this;
}
}
private function onAsyncError(event:AsyncErrorEvent):void{
trace("asyncerror");
}
private function onEnterFrame(event:Event):void{
}
public function netStatus(event:NetStatusEvent):void{
trace(event.info.code);
switch(event.info.code){
case "NetConnection.Connect.Success":
ns = new NetStream(net);
var mic:Microphone = Microphone.getMicrophone();
mic.setUseEchoSuppression(true);
mic.setSilenceLevel(0);
ns.attachAudio(mic);
try{
var cam:Camera = Camera.getCamera();
cam.setMode(271,241,15);
cam.setQuality(144000,85 );
ns.attachCamera(cam);
}catch(error:Error){
trace(error);
}
getVideo();
break;
}
}
protected function image1_clickHandler(event:MouseEvent):void
{
ExternalInterface.call("geturl");
}
private function onEnterClick(event:KeyboardEvent):void{
if(event.keyCode==13){
sendMsg();
}
}
private function onSendClick(event:Event):void{
sendMsg();
}
private function sendMsg():void{
if(msg.text==""){
Alert.show("请输入内容!");
}else{
net.call("sendToRoom",null,msg.text,roomname,username,o.selectedItem);
msg.text = "";
this.setFocus();
this.focusManager.setFocus(msg);
msg.setSelection(0,0);
}
}
public function sendToRoom(str:String):void{
c.text = c.text + str+"/n";
this.focusManager.setFocus(msg);
}
public function updateList(user:String):void{
try{
trace("update");
var arr:Array;
if(user==""){
arr = new Array();
}else{
arr = user.split(",");
}
arrayCon = new ArrayCollection(arr);
}catch(err:Error){
trace(err);
}
}
protected function userlist_itemClickHandler(event:ListEvent):void
{
if(!comboArrayCon.contains(userlist.selectedItem)){
comboArrayCon.addItem(userlist.selectedItem);
o.selectedIndex = comboArrayCon.length;
}else{
o.selectedIndex = comboArrayCon.getItemIndex(userlist.selectedItem);
}
}
private function oLabel(object:Object):String{
return object.toString();
}
public function netStream(name:String):void{
nsPlay = new NetStream(net);
nsPlay.play(name);
var video:Video = new Video(v.width,v.height);
v.addChild(video);
video.attachNetStream(nsPlay);
video.width = v.width;
video.height = v.height;
}
public function playVideo():void{
net.call("checkVideo",null,username,roomname);
}
public function getVideo():void{
net.call("sendVideo",null,username,roomname);
}
public function canVideo():void{
ns.publish(username);
w.enabled = false;
s.enabled = true;
net.call("playVideo",null,username,roomname);
}
public function canNotVideo():void{
Alert.show("有人抢先啦,慢慢等吧");
}
public function escVideo():void{
trace("escvideo");
nsPlay.close();
w.enabled = true;
s.enabled = false;
}
]]>
</mx:Script>
<mx:Label width="168" id="l"/>
<mx:TextArea width="472" height="239" id="c" editable="false" x="5" y="26"/>
<mx:TextInput x="67" y="273" width="335" id="msg" keyUp="onEnterClick(event)"/>
<mx:List x="484" y="24" width="122" height="241" id="userlist" dataProvider="{arrayCon}" itemClick="userlist_itemClickHandler(event)"/>
<mx:ComboBox x="-1" y="273" editable="false" width="60" id="o" dataProvider="{comboArrayCon}" />
<mx:Button x="414" y="273" label="send" width="63" id="send" click="onSendClick(event)"/>
<mx:Button x="485" y="273" label="我要视频" click="playVideo()" id="w"/>
<mx:VideoDisplay x="623" y="24" width="271" height="241" id="v"/>
<mx:Button x="563" y="273" label="不视频了" click="net.call('escVideo',null,roomname)" enabled="false" id="s"/>
</mx:Application>