配置好环境后,我们就可以动手写一个简单的demo了。
一,在eclipse下新建一个工程:FirstRed,将red5安装目录/red5.jar和red5安装目录/lib下的所有jar包加入到FirstRed工程中
二,新建包first在其下建立类Application
package first;
import org.red5.server.adapter.ApplicationAdapter;
import org.red5.server.api.IConnection;
public class Application extends ApplicationAdapter{
public boolean appConnect(IConnection conn,Object[] args){
return true;
}
public String getServerStr(String str){
return str+",这是从服务器返回的值";
}
}
三,在red5安装目录下找到webapps目录,进入后新建文件夹FirstRed,在FirstRed下再建立WEB-INF,然后再WEB-INF下建立classes文件夹和red5-web.properties,red5-web.xml,web.xml三个文件,
内容如下:
1,red5-web.properties文件:
webapp.contextPath=/FirstRed
webapp.virtualHosts=*
2,red5-web.xml文件:
3,web.xml
如果有安装试例程序,可以将其下的同样文件拷过来
改其中的
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>/FirstRed</param-value>
</context-param>
这里/FirstRed就是我们的工程名。
4,在classes下建立文件夹first,把刚编译过的Application.class拷到first文件夹下
到此大功告成,可以启动red54服务器,
四,编写flex客户端程序
在flex下新建一flex项目,建立mxml文件
<mx:Script>
<![CDATA[
import mx.controls.Alert;
private var rtmpURL:String="rtmp://192.168.1.180:1935/FirstRed";
private var conn:NetConnection=new NetConnection();
private var isConnectSuccess:Boolean=false;
private var responder:Responder=new Responder(resultFun);
private function resultFun(object:String):void{
trace(object);
result.text=object.toString();
}
private function clickConnect(e:MouseEvent):void{
conn.addEventListener(NetStatusEvent.NET_STATUS,netStatus);
conn.connect(rtmpURL);
Alert.show("连接成功!");
connServer.enabled=false;
}
private function click(e:MouseEvent):void{
invoke();
}
private function invoke():void{
if(isConnectSuccess){
conn.call("getServerStr",responder,str.text);//getServerStr 是服务器端方法名称
}else{
Alert.show("还没连接到服务器");
}
}
private function netStatus(e:NetStatusEvent):void{
trace(e.info.code);
if(e.info.code=="NetConnection.Connect.Success"){
isConnectSuccess=true;
}
}
]]>
</mx:Script>
<mx:Button x="224" y="175" label="调用服务器方法" click="this.click(event)"/>
<mx:TextInput x="129" y="145" id="str" width="212"/>
<mx:Label x="129" y="119" text="显示从服务器端返回的字符" id="result" width="160"
fontSize="12"/>
<mx:Button id="connServer" x="129" y="175" label="连接服务器" click="this.clickConnect(event)"/
最后运行项目: