package echat;
import org.red5.server.adapter.ApplicationAdapter;
import org.red5.server.api.IConnection;
import org.red5.server.api.IScope;
import org.red5.server.api.Red5;
import org.red5.server.api.service.IServiceCapableConnection;
import org.slf4j.Logger;
import org.red5.logging.Red5LoggerFactory;
import java.util.*;
public class Application extends ApplicationAdapter
{
private static Logger log = Red5LoggerFactory.getLogger(Application.class, "echat");
/**
15 * 每个新的客户端来连接的时候调用!
16 * 这里我们覆盖了父类的实现。
17 */
public boolean appConnect(IConnection con, Object[] params)
{
log.debug("new client connectting chat room");
return true;
}
/**
* 当客户端断开连接的时候调用!
* 这里我们覆盖了父类的实现。
*/
public void appDisconnect(IConnection conn)
{
log.debug(conn.getClient().getId() + " disconnect");
}
/**
* 加入聊天室,必须带上用户名,假如用户名为空,则不能发送消息,也不能收到消息。
* @param params 客户端调用服务器端的参数。
*/
public void jionChatRoom(Object[] params)
{
String nickName = params[0].toString();
if (null == nickName || "".equals(nickName))
{
return ;
}
else
{
// 设置用户昵称。
IConnection conn = Red5.getConnectionLocal();
conn.setAttribute("nickName", nickName);
}
// 发通知给聊天室的所有人,有新人加入了。
IScope scope = Red5.getConnectionLocal().getScope();
Iterator it = scope.getConnections().iterator();
int x = 0;
for (;it.hasNext();)
{
Set connections = (Set)it.next();
IConnection tempConn = (IConnection)connections.iterator().next();
if (tempConn instanceof IServiceCapableConnection)
{
IServiceCapableConnection sc = (IServiceCapableConnection) tempConn;
// 服务器端调用客户端flash方法。
sc.invoke("showJoinInInfo", new Object[]{nickName});
}
}
}
/**
* 给聊天室的所有人发送消息
* @param params
*/
public void sayToAll(Object[] params)
{
IConnection conn = Red5.getConnectionLocal();
//conn.setAttribute(”nickName”, nickName);
String nickName = conn.getAttribute("nickName").toString();
String sayWhat = params[0].toString();
// 发消息给聊天室的所有人.
IScope scope = Red5.getConnectionLocal().getScope();
Iterator it = scope.getConnections().iterator();
for (;it.hasNext();)
{
Set connections = (Set)it.next();
IConnection tempConn = (IConnection)connections.iterator().next();
if (tempConn instanceof IServiceCapableConnection)
{
IServiceCapableConnection sc = (IServiceCapableConnection) tempConn;
// 服务器端调用客户端flash方法。
sc.invoke("showMessage", new Object[]{nickName+ " : " +sayWhat});
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<mx:Application fontSize="12" xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
private var nc:NetConnection;
private function connectAndJoinRoom(e:Event):void
{
var nickName:String = nickNameTextInput.text;
if (nickName == "")
{
return;
}
else
{
if (nc == null)
{
initialNetConnection();
}
}
}
private function initialNetConnection():void
{
nc = new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS, connectStatus);
nc.client = this;
nc.connect("rtmp://localhost/echat", null);
}
private function connectStatus(event:NetStatusEvent) : void
{
if (event.info.code == "NetConnection.Connect.Success")
{
nc.call("jionChatRoom", null, nickNameTextInput.text);
sendButton.enabled = true;
}
}
private function sendMessage():void
{
var sendString:String = inputWhatYouWantSay.text;
inputWhatYouWantSay.text = "";
nc.call("sayToAll", null, sendString);
}
public function showJoinInInfo(message:String) : void
{
roomArea.text += message + " 加入聊天室" + "/n";
}
public function showMessage(message:String) : void
{
roomArea.text += message + "/n";
}
]]>
</mx:Script>
<mx:VBox x="20" y="20">
<mx:HBox>
<mx:Label text="昵称 : "></mx:Label>
<mx:TextInput id="nickNameTextInput">
</mx:TextInput>
<mx:Button click="connectAndJoinRoom(event)" label="加入聊天室">
</mx:Button>
</mx:HBox>
<mx:HBox width="100%" height="300">
<mx:TextArea height="100%" width="100%" id="roomArea">
</mx:TextArea>
</mx:HBox>
<mx:HBox width="100%">
<mx:TextInput width="100%" id="inputWhatYouWantSay">
</mx:TextInput>
<mx:Button enabled="false" id="sendButton" label="发送" click="sendMessage()">
</mx:Button>
</mx:HBox>
</mx:VBox>
</mx:Application>
red5 服务器端与客户端实例
最新推荐文章于 2024-08-09 07:40:59 发布