开发者

AS3 ByteArray " The supplied index is out of bounds" Error

开发者 https://www.devze.com 2023-03-26 18:04 出处:网络
I\'m working on an application and I need to save an AS3 object to a db. Here\'s what I\'m doing: private function getComplete(e:Event)

I'm working on an application and I need to save an AS3 object to a db. Here's what I'm doing:

private function getComplete(e:Event) 
{

    var getVars:URLVar开发者_运维技巧iables = new URLVariables(unescape(e.target.data));
    var _saveData_obj =  readObjectFromStringBytes( getVars.saveData);

    // do something with the save data.... 

}

public function SaveGame() {    
    var _save_data:Object = _puzzle.dataForSaving;
    var _serialized_string = escape(serializeToString(_save_data));
    var _round_time = Math.round( _elapsed_time);
    var _token =  MD5.hash( _id + 
                            _difficulty + 
                            "mysomewhatsecretstringhere" + 
                            _round_time );

    var request:URLRequest = 
             new URLRequest( _home + 'savegame.php' );
    request.method = URLRequestMethod.POST;
    var variables:URLVariables = new URLVariables();   
    variables.saveData = _serialized_string;     
    variables.time = _round_time;
    variables.id = _id;
    variables.dif = _difficulty;
    variables.token = _token;
    request.data = variables;
    var loader:URLLoader = new URLLoader (request);
    loader.addEventListener(Event.COMPLETE, postComplete);
    loader.dataFormat = URLLoaderDataFormat.TEXT;
    loader.load(request); 

}

public function LoadGame() {

    var request:URLRequest = 
        new URLRequest( _home + 'loadgame.php?id='+_id+"&dif="+_difficulty);
    request.method = URLRequestMethod.GET;

    var loader:URLLoader = new URLLoader (request);
    loader.addEventListener(Event.COMPLETE, getComplete);
    loader.addEventListener(IOErrorEvent.IO_ERROR, netError);
    loader.dataFormat = URLLoaderDataFormat.TEXT;

    loader.load(request); 

}    

public static function serializeToString(value:Object):String{
    if(value==null){
        throw new Error("null isn't a legal serialization candidate");
    }

    var bytes:ByteArray = new ByteArray();
    bytes.writeObject(value);
    bytes.position = 0;
    trace ("Saved: "+bytes.length);
    var be:String = Base64.encodeByteArray(bytes);

    return be;
}

public static function readObjectFromStringBytes(value:String):Object {
    var result:ByteArray=Base64.decodeToByteArray( value) as ByteArray;
    result.position = 0;
    var the_obj:Object = result.readObject();
    return the_obj
} 

The problem is that I keep getting a "The supplied index is out of bounds" error when I try to read the object from the Base64 string.... I checked if the saved string and the loaded string are the same. Tried to save the Base64 string to a shared object and retrieve it - that works fine ... the only problem is when I save and load to/from the server.

Can you guys help me? What am I doing wrong?

Thanks.


try using ba.writeMultiByte(string, 'utf-8') and ba.readMultiByte(ba.bytesAvailable, 'utf-8') without converting to object


Ok, so it looks like we've come to a series of steps you can take to debug what's going on.

  1. (In save game) make sure that unescape( variables ).saveData == _puzzle.dataForSaving. If this does not work, then your issue is with either escape or unescape. I have to admit
    • I am suspicious here, you should never need to escape data for a POST once it has been Base64 encoded (as3crypto's Base 64 encoding will return only one of these characters: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=. Those are all legit in a POST) and your URLLoader should take care of making it a legit request, so I have difficulty seeing the need.
    • It is more consistent (and more expected) to have escape and unescape both accept strings as parameters and return strings.
  2. Instead of storing to the database and then retrieving, store to $_SESSION (or, if possible, just echo the value back). This will eliminate PHP as a possible culprit.
  3. In PHP, make sure that $_POST['saveData'] == /* whatever you echo */. If it isn't, then you have a DB encoding issue. You need to make sure that whatever encoding is used in MySQL is the same as the one used in AS.

As an aside, when working with AS, you'll often find it easier to use $_REQUEST instead of $_POST or $_GET. Since the requests are more or less abstracted, the URLRequests of AS don't really need to worry about being RESTful, and it is a heck of a lot easier to work with than exclusively using POSTs.

0

精彩评论

暂无评论...
验证码 换一张
取 消