使用Red5 Flash Server的简单远程共享对象

时间:2009-10-09 23:13:21

标签: java actionscript-3 red5 shared-objects

我正在尝试使用red5媒体服务器创建一个简单的聊天客户端,但我似乎有轻微的打嗝。我在服务器端创建一个共享对象,它似乎正在成功创建它。但是,当我通过客户端更改对象(键入消息)时,会触发SYNC事件,但共享对象中的内容仍为空。我怀疑我在java端做错了什么,有什么建议吗?

控制台结果:

Success!

Server Message: clear

Server Message: [object Object]

Local message: asdf

Server Message: change

Server Message: [object Object]

Local message: fdsa

Server Message: change

Server Message: [object Object]

Local message: fewa

Server Message: change

Server Message: [object Object] 

服务器端:

    package org.red5.core;

import java.util.List;

import org.red5.server.adapter.ApplicationAdapter;
import org.red5.server.api.IConnection;
import org.red5.server.api.IScope;
import org.red5.server.api.service.ServiceUtils;
import org.red5.server.api.so.ISharedObject;
// import org.apache.commons.logging.Log;
// import org.apache.commons.logging.LogFactory;

public class Application extends ApplicationAdapter {
    private IScope appScope;
    // private static final Log log = LogFactory.getLog( Application.class );

    /** {@inheritDoc} */
    @Override
    public boolean connect(IConnection conn, IScope scope, Object[] params) {

        appScope = scope;
        createSharedObject(appScope, "generalChat", false); // Creates general chat shared object

        return true;
    }

    /** {@inheritDoc} */
    @Override
    public void disconnect(IConnection conn, IScope scope) {
        super.disconnect(conn, scope);
    }  

    public void updateChat(Object[] params)
    {
        ISharedObject so = getSharedObject(appScope, "generalChat"); // Declares and stores general chat data in general chat shared object
        so.setAttribute("point", params[0].toString());
    } 
}

客户端:

package
{
    import flash.display.MovieClip;
    import flash.events.*;
    import flash.net.*; 


    // This class is going to handle all data to and from from media server
    public class SOConnect extends MovieClip
    {
        // Variables
        var nc:NetConnection = null;
        var so:SharedObject;        
        public function SOConnect():void
        {
        }   
        public function connect():void
        {       
            // Create a NetConnection and connect to red5
            nc = new NetConnection();
            nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
            nc.connect("rtmp://localhost/testChat");

            // Create a StoredObject for general chat
            so = SharedObject.getRemote("generalChat", nc.uri, false);          
            so.connect(nc);
            so.addEventListener(SyncEvent.SYNC, receiveChat)            
        }

        public function sendChat(msg:String)
        {
            trace ("Local message: " + msg);
            nc.call("updateChat", null, msg)                    
        }


        public function receiveChat(e:SyncEvent):void
        {           
            for (var i in e.changeList)
            {
                trace ("Server Message: " + e.changeList[i].code)
                trace ("Server Message: " + e.changeList[i])
            }                       
        }

        // Given result, determine successful connection        
        private function netStatusHandler(e:NetStatusEvent):void
        {
            if (e.info.code == "NetConnection.Connect.Success")
            {
                trace("Success!");
            }
            else 
            {
                trace("Failure!\n");    
                trace(e.info.code);
            }
        }
    }
}

2 个答案:

答案 0 :(得分:5)

你不需要在服务器端写任何东西就可以在as3和red5中进行聊天;

以下是您the chat that it is working and it is written in as3/flex3

的示例

答案 1 :(得分:0)

是的,可以创建聊天而无需编写服务器端代码,但我不知道有人可以控制用户断开连接或使用同步用户列表,无论如何回到主题可能你有dir权限问题所以尝试运行red5作为root / admin一次,只是检查它是否有效,如果是,你应该在系统中创建一个用户(具有正确的写入权限),然后使用该用户运行red5。