關於服務端的發佈很簡單,按照官網上介紹的,將blazeds.war下下來後,按照示例建一個blazeds的web project
下面是我project目錄的截圖
其中可能需要改動的就是remoting-config.xml和web.xml兩個文件,其他配置文件默認不需要改動
我的remoting-config.xml如下
<?xml version="1.0" encoding="UTF-8"?> <service id="remoting-service" class="flex.messaging.services.RemotingService"> <adapters> <adapter-definition id="java-object" class="flex.messaging.services.remoting.adapters.JavaAdapter" default="true"/> </adapters> <default-channels> <channel ref="my-amf"/> </default-channels> <destination id="helloWorld"> <properties> <source>svr.HelloWorld</source> </properties> </destination> <destination id="bdserver"> <properties> <source>svr.BDServer</source> </properties> </destination> </service>
我的web.xml內容如下:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <display-name>BlazeDS</display-name> <description>BlazeDS Application</description> <!-- Http Flex Session attribute and binding listener support --> <listener> <listener-class>flex.messaging.HttpFlexSession</listener-class> </listener> <!-- MessageBroker Servlet --> <servlet> <servlet-name>MessageBrokerServlet</servlet-name> <display-name>MessageBrokerServlet</display-name> <servlet-class> flex.messaging.MessageBrokerServlet </servlet-class> <init-param> <param-name>services.configuration.file</param-name> <param-value>/WEB-INF/flex/services-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet> <servlet-name>InitServlet</servlet-name> <servlet-class>servlets.InitServlet</servlet-class> <load-on-startup>3</load-on-startup> </servlet> <!-- RDSDispatchServlet相關配置 默認是註釋掉的,如果需要Flash Builder通過data/service服務能連接的話,這邊需要去掉註釋 --> <servlet> <servlet-name>RDSDispatchServlet</servlet-name> <display-name>RDSDispatchServlet</display-name> <servlet-class>flex.rds.server.servlet.FrontEndServlet</servlet-class> <init-param> <param-name>useAppserverSecurity</param-name> <param-value>false</param-value><!-- 此處默認值是true,通過Flash Builder來生成相關服務時,需改爲false,即不同相關用戶名、密碼來驗證 --> </init-param> <load-on-startup>10</load-on-startup> </servlet> <servlet-mapping id="RDS_DISPATCH_MAPPING"> <servlet-name>RDSDispatchServlet</servlet-name> <url-pattern>/CFIDE/main/ide.cfm</url-pattern> </servlet-mapping> <!-- 以上是RDSDispatchServlet相關配置 --> <servlet-mapping> <servlet-name>MessageBrokerServlet</servlet-name> <url-pattern>/messagebroker/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> </welcome-file-list> <!-- 1111 for WebSphere deployment, please uncomment --> <!-- <resource-ref> <description>Flex Messaging WorkManager</description> <res-ref-name>wm/MessagingWorkManager</res-ref-name> <res-type>com.ibm.websphere.asynchbeans.WorkManager</res-type> <res-auth>Container</res-auth> <res-sharing-scope>Shareable</res-sharing-scope> </resource-ref> --> </web-app>
至此發佈,服務端就算ok了,
remoteobject:
主要代碼如下:
<s:ChannelSet id="channelSet"> <s:AMFChannel id="amfchannel" url="http://192.168.6.19:8008/blazeds/messagebroker/amf"/> </s:ChannelSet> <s:RemoteObject id="ro" destination="bdserver" channelSet="{channelSet}" result="onResult(event)" fault="onFault(event)"> </s:RemoteObject>
這樣通過ro這個對象就可以直接調用其發佈的方法了
連接blazeds生成相關代理文件:
此處需要注意的是如果你的客戶端的項目的context root和服務端的webcontext不一致的話就不能生成,會報錯,拿我的這個做個列子,我服務端的webcontext是blazeds,而現在客戶端的項目webcontext是JNBus,這樣會報錯,如果你客戶端的目錄名和服務器的一致就沒問題了,但如果不一致的話咋辦呢?其實可以臨時調整下這個flex project 的context root,改完生成服務代理之後再調整過來,就ok了,改的地方如下圖, root url和context root的JNBus換成blazeds
生成之後會自動生成如下的東西:
這樣代理文件就算生成好了,下面就看怎麼用了,下面是主要代碼:
<fx:Declarations> <s:ChannelSet id="channelSet"> <s:AMFChannel id="amfchannel" url="http://192.168.6.19:8008/blazeds/messagebroker/amf"/> </s:ChannelSet> <s:CallResponder id="getCarCountResult" result="getCarCountResult_resultHandler(event)" /> </fx:Declarations> private function init(): void { this.bdServer = new Bdserver(); this.bdServer.channelSet = this.channelSet; this.getCarCountResult.token = this.bdServer.getCarCount(); }
這樣總算可以了