Red5研究与积累(二)
做成功了第一个连接服务端的简单例子,现在我们来做一个客户端调用服务端方法的例子
客户端依然选择Flex
新建一个Web工程
配置好环境和配置文件
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>red5Demo1</display-name>
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>/red5Demo1</param-value>
</context-param>
<listener>
<listener-class>org.red5.logging.ContextLoggingListener</listener-class>
</listener>
<filter>
<filter-name>LoggerContextFilter</filter-name>
<filter-class>org.red5.logging.LoggerContextFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>LoggerContextFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>rtmpt</servlet-name>
<servlet-class>org.red5.server.net.rtmpt.RTMPTServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>rtmpt</servlet-name>
<url-pattern>/fcs/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>rtmpt</servlet-name>
<url-pattern>/open/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>rtmpt</servlet-name>
<url-pattern>/close/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>rtmpt</servlet-name>
<url-pattern>/send/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>rtmpt</servlet-name>
<url-pattern>/idle/*</url-pattern>
</servlet-mapping>
<security-constraint>
<web-resource-collection>
<web-resource-name>Forbidden</web-resource-name>
<url-pattern>/streams/*</url-pattern>
</web-resource-collection>
<auth-constraint/>
</security-constraint>
</web-app>
red5-web.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.0.xsd">
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="/WEB-INF/red5-web.properties" />
</bean>
<bean id="web.context" class="org.red5.server.Context"
autowire="byType" />
<bean id="web.scope" class="org.red5.server.WebScope"
init-method="register">
<property name="server" ref="red5.server" />
<property name="parent" ref="global.scope" />
<property name="context" ref="web.context" />
<property name="handler" ref="web.handler" />
<property name="contextPath" value="${webapp.contextPath}" />
<property name="virtualHosts" value="${webapp.virtualHosts}" />
</bean>
<bean id="web.handler" class="org.red5.demos.Application" />
<bean class="org.springframework.scripting.support.ScriptFactoryPostProcessor"/>
</beans>
red5-web.properties
webapp.contextPath=/red5Demo1
webapp.virtualHosts=*, localhost, localhost:8088, 127.0.0.1:8088
新建一个Java 类 起名 Application 继承 ApplicationAdapter
public class Application extends ApplicationAdapter {
private IScope appScope;
@Override
public boolean appConnect(IConnection conn, Object[] params) {
System.out.println(" app connect ");
return true;
}
@Override
public void appDisconnect(IConnection conn) {
System.out.println(" app disconnect ");
super.appDisconnect(conn);
}
@Override
public boolean appStart(IScope arg0) {
System.out.println(" app start ");
this.scope = arg0;
return true;
}
public String serverMethod(String str) {
System.out.println(" server method invoke.. ");
return str.toUpperCase();
}
}
其中 serverMethod 为给客户端调用的方法 该方法调用时打印一句话 并且将传入的字符串以大写形式返回给客户端
在Red5 webapps 文件夹下新建 red5Demo1 文件夹 将项目部署
启动Red5 服务器
接下来是客户端代码
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
private var rtmpurl:String = "rtmp://localhost/red5Demo1";
private var conn:NetConnection = new NetConnection();
private var isConnectSuccess:Boolean = false;
private var resp:Responder = new Responder(resFun);
private function resFun(obj:String):void {
resIn.text = obj;
}
private function invoke():void {
if(isConnectSuccess){
conn.call("serverMethod", resp , textIn.text);
}else{
Alert.show("还没连接到服务器");
}
}
private function clickConnect(e:MouseEvent):void {
conn.addEventListener(NetStatusEvent.NET_STATUS, netStatus);
conn.connect(rtmpurl);
}
private function netStatus(e:NetStatusEvent):void {
if(e.info.code == "NetConnection.Connect.Success"){
isConnectSuccess = true;
}
}
]]>
</mx:Script>
<mx:Button x="224" y="175" label="Invoke" click="invoke()" />
<mx:TextInput x="129" y="145" width="212" id="textIn" />
<mx:Label x="129" y="119" text="" fontSize="12" width="160" id="resIn" />
<mx:Button x="129" y="175" label="Connect" click="clickConnect(event)" />
</mx:Application>
点击Connect 按钮连接上服务器
接着在输入框输入英文字母 例如 abc
点击 Invoke 按钮 即可看到 Label 中文字为输入的英文的大写字符串
到此客户端调用服务端方法的例子结束