从flex应用程序连接到red5远程共享对象

时间:2012-12-24 09:45:41

标签: java flex red5 shared-objects

我正在编写Flex应用程序。

服务器端应用程序是位于Red5 1.0之上的Java应用程序。 该连接是通过RTMPE protcol完成的。 (结果与RTMP或RTMPE相同)。

我能够正确连接到服务器并进入和离开房间,但我无法连接到房间中的共享对象。使用非持久性共享对象。

我使用以下代码在服务器端的房间中创建共享对象:

private void createSharedObject (IScope scope, String soName, boolean persistent) {
    ISharedObjectService service= (ISharedObjectService) ScopeUtils
            .getScopeService(scope,
                    ISharedObjectService.class,
                    false);
    service.createSharedObject(scope, soName, persistent);
}

private ISharedObject getSharedObject(IScope scope, String soName,boolean persistent) {
    ISharedObjectService service = (ISharedObjectService) ScopeUtils
            .getScopeService(scope,
                    ISharedObjectService.class,
                    false);
    return service.getSharedObject(scope, soName,persistent);
}

   public ISharedObject getRoomSharedObject() {
        final String soName = "room_" + this._scope.getName();

            log.debug("application found. creating shared object");
                log.debug("shared object: {} not found for scope: {}. creating one",new Object[]{soName,this._scope.getContextPath()});
            createSharedObject(this._scope, soName, XpoConstants.persistentSharedObjects);
            ISharedObject so = getSharedObject(this._scope,soName, XpoConstants.persistentSharedObjects);
            so.clear();

            return so;

    }      

然后用户进入正确的房间我得到了连接,并在他的末尾调用一个函数并提供共享对象名称,以便他知道要使用以下代码连接到什么:

    IConnection conn = Red5.getConnectionLocal();
    IServiceCapableConnection sc = (IServiceCapableConnection) conn;
    sc.invoke("<function name>", new Object[]{this._so.getName(),...});

现在在客户端, 我在flex应用程序中使用以下代码来获取远程共享对象:

var roomSharedObject:SharedObject = SharedObject.getRemote(soName,   SharedUtils.getNetConnection().uri, Finals.persistentSharedObject);

roomSharedObject.addEventListener(NetStatusEvent.NET_STATUS, this._parse);

roomSharedObject.addEventListener(SyncEvent.SYNC,this._parse2);
roomSharedObject.connect(SharedUtils.getNetConnection());

...

    private function _parse(e:NetStatusEvent):void {
        trace("NETSTATUSEVENT");
        trace("########## EVENT INFO CODE: " + e.info.code);
    }

    private function _parse2(e:SyncEvent):void {
        trace("SYNCEVENT");

    }

我从red5获得的唯一消息如下:

[INFO] [pool-8-thread-12] org.red5.server.so.SharedObject - Deleting shared object room_3963 because all clients disconnected and it is no longer acquired.

我猜想应该触发NetStatusEvent会出现某种错误信息 但我根本没有跟踪消息!我从red5获得的唯一一件事就是删除了共享对象,这意味着该客户端试图连接一段时间并断开连接。

我厌倦了google,厌倦了red5 google组。我迷路了! 任何有关该问题的信息都将不胜感激。

谢谢你!

更新

好吧我创建了一个较小的客户端和服务器..

在服务器上,appConnect函数只返回true。

客户端是flex 4.6客户端,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" creationComplete="init()"
minHeight="600">
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>

<fx:Script>
    <![CDATA[
    import mx.controls.Alert;

    private var nc:NetConnection = new NetConnection();

    private function init():void {

        nc.proxyType = "best";
        nc.objectEncoding = ObjectEncoding.AMF0;
        nc.addEventListener (NetStatusEvent.NET_STATUS,checkConnect);                    
        nc.connect("rtmpe://127.0.0.1/ufktest");

    }

    private function checkConnect (event:NetStatusEvent):void
    {
        switch (event.info['code']) {
            case 'NetConnection.Connect.Success':
            {   
                _red5Connected();
                trace('connected succesfully');
                break;
            }
            case 'NetConnection.Connect.Closed':
            {   
                _red5Disconnected();
                trace('connection closed');
                break;
            }
            case 'NetConnection.Connect.Rejected':
            {
                _red5Disconnected();
                trace('connection rejected' );
                break;
            }
            case 'NetConnection.Connect.Failed':
            {
                _red5Disconnected();
                trace('connection failed');
                break;
            }
            case 'NetConnection.Connect.AppShutDown':
            {
                trace('app shut down');
                _red5Disconnected();
                break;
            }
            default:
            {
                trace("unknown netconenction status: " + event.info['code']);                   
                break;
            }               
        }

    }

        private function _red5Connected():void {
            trace("red5 connection was successful");
            var roomSharedObject:SharedObject = SharedObject.getRemote("moshe",nc.uri,false);
            roomSharedObject.addEventListener(NetStatusEvent.NET_STATUS, _parse);
            roomSharedObject.addEventListener(SyncEvent.SYNC,_parse2);
            roomSharedObject.connect(nc);


        }
    private function _parse(e:NetStatusEvent):void {
        trace("NETSTATUSEVENT");
        trace("########## EVENT INFO CODE: " + e.info.code);
    }

    private function _parse2(e:SyncEvent):void {
        trace("SYNCEVENT");

    }

        private static function _red5Disconnected():void {
            trace("disconnected from red5");
            Alert.show("could not connect to server, please try again later");
        }


    ]]>
</fx:Script>

仍然在我运行客户端应用程序时它连接到服务器并给我以下错误:

[INFO] [pool-8-thread-15] org.red5.server.so.SharedObject - Deleting shared object moshe because all clients disconnected and it is no longer acquired.

2 个答案:

答案 0 :(得分:1)

还有日志吗?但是,您的日志表明共享对象是在服务器上成功创建的。尝试使用acquire()方法创建roomStart()内的共享对象或创建它的任何位置(roomStart()应该是正确的位置虽然)。如果没有连接客户端,默认情况下服务器将销毁任何共享对象。

答案 1 :(得分:0)

这似乎是一种过于复杂的方式。如果您使用Flex方法SharedObject::getRemote,所有这些都将为您完成。如果对象存在,客户端将获得一个副本。如果它不存在,则将创建该对象。因此,“房间”中的第一个人创建了该对象,之后出现的任何人都将获得一份副本。