As3下的通信(Red5、SharedObject)

作者: nick 分类: as, flash, java, Red5 发布时间: 2010-09-27 10:50 ė 61条评论

简介摘要:代 码如下: client: FLASH var nc:NetConnection = new NetConnection(); nc.client = this; nc.objectEncoding = flash.net.ObjectEncoding.AMF0;//注意这句话(不加的话  同步事件没有被触发) nc.connect("rtmp://19

代码[dai ma]如下:

client:

FLASH

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

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(“连接[lian jie]成功!”);
}
if (Evt.info.code == “NetConnection.Connect.Failed”) {
history.appendText(“连接[lian jie]失败!”);
}
}

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
//该方法[fang fa]初始化[chu shi hua]时执行[zhi hang]一次
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”>
<property name=”location” value=”/WEB-INF/red5-web.properties” />
</bean>
<bean id=”web.context”
autowire=”byType” />
<bean id=”web.scope”
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”

singleton=”true” />

<!– this will support calling methods through “myhandler.<methodName>”
<bean id=”myhandler.service”

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;//注意这句话(不加的话  同步[tong bu]事件[shi jian]没有被触发)

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

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

javascript 与 flash 互通[hu tong]

4. javascript 到 flash

//加载swf 后面的参数[can shu]列表[lie biao]
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

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

<!– 通信[tong xin]机制[ji zhi]代码[dai ma]开始
<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>

通信[tong xin]机制[ji zhi]代码[dai ma]结束 –>

id一定要加 不然 IE下调用[tiao yong]不了 Javascript 脚本[jiao ben] 报错为 : NULL为空或不是对象[dui xiang]

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

使用javascript调用[tiao yong]Flash中的方法[fang fa]

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

本文出自 传播、沟通、分享,转载时请注明出处及相应链接。

本文永久链接: https://www.nickdd.cn/?p=1066

发表评论

您的电子邮箱地址不会被公开。

Ɣ回顶部