views:

69

answers:

3

Hi, I'm using AMFPHP 1.9, ActionScript 3 and Adobe Flash Pro CS5 with Flash player 10x. I was trying to get data by calling amfphp serices. I'm running the app from within the Flash IDE.

For primitive types like string or integer it's working fine. But when trying to get a custom object it shows the following error:

SecurityError: Error #2000: No active security context.

And the parameter in the result handler function contains null. Did I miss anything here? Please help.

Following are my classes:

ServiceContext.as

import flash.net.NetConnection;
import flash.net.ObjectEncoding;
import flash.events.NetStatusEvent;

internal class ServiceContext
{
    protected var myService:NetConnection;

    public function ServiceContext():void
    {
        myService = new NetConnection();
        myService.objectEncoding = ObjectEncoding.AMF3;
        myService.connect("http://localhost/MyApp/amfphp/gateway.php");
        myService.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
    }

    private function onNetStatus(event:NetStatusEvent):void
    {
        trace(event.info);            
    }
}

SceneService.as

import com.ddr.cv.model.Scene;
import flash.net.Responder;    

public class SceneService extends ServiceContext
{
    public function SceneService():void
    {
        com.ddr.cv.model.Scene.register();
    }

    public function getSceneByID(sceneID:uint, resultHandler:Function, faultHandler:Function)
    {
        var responder = new Responder(resultHandler, faultHandler);
        myService.call("com.ddr.cv.model.Scene.getSceneByID", responder, sceneID);
    }

    public function saveScene(scene:Scene, resultHandler:Function, faultHandler:Function)
    {
        var responder = new Responder(resultHandler, faultHandler);
        myService.call("Scene.saveScene", responder, scene);
    }
}

Scene.as

    import flash.display.MovieClip;
    import flash.net.registerClassAlias;

    [RemoteClass(alias="com.ddr.cv.model.Scene")]
    [Bindable]
    public class Scene extends MovieClip
    {

        private var _id:uint;
        public function get id():uint
        {
            return _id;
        }

        private var _sceneName:String;
        public function get sceneName():String
        {
            return _sceneName;
        }

        private var _imageName:String;
        public function get imageName():String
        {
            return _imageName;
        }

        private var _sceneCategoryID:int;
        public function get sceneCategoryID():int
        {
            return _sceneCategoryID;
        }

        private var _userID:String;
        public function get userID():String
        {
            return _userID;
        }

        private var _creationDate:Date;
        public function get creationDate():Date
        {
            return _creationDate;
        }

        public function Scene(id:uint = 0, sceneName:String = null, imageName:String = null, sceneCategoryID:int = 0, userID:String = null, creationDate:Date = null):void
        {
            _id = id;
            _sceneName = sceneName;
            _imageName = imageName;
            _sceneCategoryID = sceneCategoryID;
            _userID = userID;
            _creationDate = creationDate;
        }

        public static function register():void
        {
            registerClassAlias("com.ddr.cv.model.Scene", com.ddr.cv.model.Scene) ;
        }
}

and here's Scene.php

    <?php
class Scene {

    var $id;
    var $sceneName;
    var $imageName;
    var $sceneCategoryID;
    var $userID;
    var $creationDate;

    // explicit actionscript package
    var $_explicitType = "com.ddr.cv.model.Scene";

    function Scene($id = 0, $sceneName = null, $imageName = null, $sceneCategoryID = 0, $userID = null, $creationDate = null)
    {
        $this->id = $id;
        $this->sceneName = $sceneName;
        $this->imageName = $imageName;
        $this->sceneCategoryID = $sceneCategoryID;
        $this->userID = $userID;
        $this->creationDate = $creationDate;
    }

    function getSceneByID($id){
        //creating dummy Scene object
        $scene = new Scene(1, "Test Scene", "test_scene.jpg", 6, null, null);
        return $scene;
    }

    function saveScene($scene)
    {
        //To Do:
    }
}
?>

Here's how I call the service:

var sceneService:SceneService = new SceneService();
sceneService.getSceneByID(1, getSceneByID_resultHandler, getSceneByID_faultHandler);

Handlers:

    public function getSceneByID_resultHandler(scene:com.ddr.cv.model.Scene):void
    {
        //scene contains null here.
        //Shouldn't it contain the dummy Scene object from amfphp service method?
        trace("Success: " + scene);
    }

    public function getSceneByID_faultHandler(fault:Object):void
    {
        trace(">>> fault:" + fault.description);
    }
+1  A: 

Try running from within a browser, I found AMFPHP / Flash act funny inside the IDE.

daidai
+1  A: 

@daidai: Thanks very much that you replied. I thought this post would reside forever deep inside the SOF database unanswered/ non-replied and never see light. And then, after 10 long days, you replied :) I upped your answer because you cared to reply. Thanks once again.

I tested it from browser too. Same result.

But I finally figured out why it was not working several days back. The problem is, for some strange (or security) reason, custom AS3 classes that inherit from MovieClip (and I guess for all system classes) cannot be mapped from their PHP counterparts, in my case, from the Scene.php class. But the AS3 class can be sent to the PHP service class and be successfully mapped. I had to bring in some changes to my architecture to address this issue and solved my problem. But this problem really put me on edge and I kept this post under hourly watch for days in hope of some reply.

Anyway, I only tested it on Flash player version 10.1.82.76. I don't know if it works in the previous versions as I did not test on them.

Kayes
A: 

Hi Kayes,

I'm having the same issue here. Would you mind sharing what you have done to resolve it?

Thanks!

Tom