As3下的通信(Red5)

来源:互联网 发布:编程好书 编辑:程序博客网 时间:2024/05/19 15:24

代码如下:

client:

FLASH

var nc:NetConnection = new NetConnection();
nc.client = this;
nc.objectEncoding = flash.net.ObjectEncoding.AMF0;//注意这句话(不加的话  同步事件没有被触发)

nc.connect("rtmp://192.168.1.109/wxgameflashserver");

var so:SharedObject = SharedObject.getRemote("gamemsg", nc.uri, false);
so.connect(nc);

nc.addEventListener(NetStatusEvent.NET_STATUS,netStatusHandler);
so.addEventListener(SyncEvent.SYNC,newMessageHandler);
sendButton.addEventListener(MouseEvent.CLICK,sendMessage);

function netStatusHandler(Evt:NetStatusEvent){

        if (Evt.info.code == "NetConnection.Connect.Success") {
              history.appendText("连接成功!");
        }
        if (Evt.info.code == "NetConnection.Connect.Failed") {
              history.appendText("连接失败!");
        }
}

function newMessageHandler(event:SyncEvent):void{
    var infoObj:Object=event.changeList;
    for (var i = 0; i<infoObj.length; i++) {
        var info = infoObj[i];
        if (info.name != undefined) {
             if(info.code=="change"){
                  history.appendText(so.data.chatMessage + "/n");
             }
        }
    }
}

function sendMessage(event:MouseEvent):void{
    so.setProperty("chatMessage",message.text);
    so.setDirty("chatMessage");
    history.appendText(message.text + "/n");
    message.text="";
}

 

Server:

java

Application.java

package com.zhangchao;

import org.red5.server.adapter.ApplicationAdapter;
import org.red5.server.api.IScope;
import org.red5.server.api.so.ISharedObject;

public class Application extends ApplicationAdapter{

    private ISharedObject iso;
    private String chatMessage = "" ;
    public String getChatMessage() {
        return chatMessage;
    }

    public void setChatMessage(String chatMessage) {
        this.chatMessage = chatMessage;
    }

    @Override
    //该方法初始化时执行一次
    public synchronized boolean start(IScope scope) {
        this.createSharedObject(scope, "gamemsg", true);
        iso = this.getSharedObject(scope, "gamemsg",false);// persistent==false
        iso.beginUpdate();
        iso.setAttribute("chatMessage", chatMessage);
        iso.endUpdate();
        return super.start(scope);
    }
}

 

=================================================/

red5-web.properties

webapp.contextPath=/wxgameflashserver
webapp.virtualHosts=*,localhost,127.0.0.1,192.168.1.109

=================================================/

red5-web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <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="com.zhangchao.Application"
        singleton="true" />

    <!-- this will support calling methods through "myhandler.<methodName>"
    <bean id="myhandler.service"
        class="the.path.to.my.ServiceHandler"
        singleton="true" />-->

</beans>

===================================================/

web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app
   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"
   version="2.4">

    <display-name>My sample Red5 application</display-name>

    <context-param>
        <param-name>globalScope</param-name>
        <param-value>default</param-value>
    </context-param>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/red5-*.xml</param-value>
    </context-param>

    <context-param>
        <param-name>locatorFactorySelector</param-name>
        <param-value>red5.xml</param-value>
    </context-param>

    <context-param>
        <param-name>parentContextKey</param-name>
        <param-value>default.context</param-value>
    </context-param>
    <context-param>
        <param-name>webAppRootKey</param-name>
        <param-value>/wxgameflashserver</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- remove the following servlet tags if you want to disable remoting for this application -->
    <servlet>
        <servlet-name>gateway</servlet-name>
        <servlet-class>org.red5.server.net.servlet.AMFGatewayServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>gateway</servlet-name>
        <url-pattern>/gateway</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>

 

 

需要注意的几个地方:

1.iso = this.getSharedObject(scope, "gamemsg",false);// persistent==false

2.nc.objectEncoding = flash.net.ObjectEncoding.AMF0;//注意这句话(不加的话  同步事件没有被触发)

3.Flash player 9  与10.0 播放器也有区别

===================================================

javascript 与 flash 互通

4. javascript 到 flash

//加载swf 后面的参数列表
var roleid:String;
roleid=loaderInfo.parameters["roleid"];

5. flash 到 javascript 

> getURL("javascript:quickreg();");   As2

> getURL(http://www.163.com/, "_parent");  As2

>navigateToURL(new URLRequest(http://www.baidu.com)); As3

 

=======================================================

<!-- 通信机制代码开始
<object id="objieff"  classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="500" height="300" codebase="http://active.macromedia.com/flash5/cabs/swflash.cab#version=5,0,0,0">
    <param name="movie" value="http://192.168.1.109:5080/wxgameflashserver/comm.swf?roleid=888">
    <param name="play" value="true">
    <param name="quality" value="high">
    <param name="scale" value="noborder">
    <embed src="http://192.168.1.109:5080/wxgameflashserver/comm.swf?roleid=888"
        width="500"
        height="300"
        play="true"
        loop="true"
        quality="high"
        scale="noborder"
        pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?p1_prod_version=shockwaveflash">
    </embed>
</object>

通信机制代码结束 –>

id一定要加 不然 IE下调用不了 Javascript 脚本 报错为 : NULL为空或不是对象

=============================================

使用javascript调用Flash中的方法

js:
var flash = document.getElementById( "newFlash" );
flash.showNanyhXG();
Flash:
ExternalInterface.addCallback ("showNanyhXG", this, showNanyhXG);