I am trying to pass object to the server through socket connection in actionscript 3. What is the best way to do that?
is serialization better?
or should I encode it first and then sent it as string?
please help me to understand this?
thank you
If your object is implementing IExternalizable and you call registerClassAlias you are safe to use readObject and writeObject. Note however that no constructor parameters are allowed when implementing IExternalizable.
For instance:
package {
import flash.net.*;
import flash.utils.*;
public class Foo implements IExternalizable {
registerClassAlias("Foo", Foo);
public var bar: String;
public function Foo() { // No constructor parameters allowed.
}
public function writeExternal(output: IDataOutput): void { output.writeUTF(bar); }
public function readExternal(input: IDataInput): void { bar = input.readUTF(); }
}
}
You are then safe to call readObject and writeObject on any IDataOutput or IDataInput which is for instance a Socket, ByteArray or URLStream.
Related
I'm trying to call a static method from a class called "JSON", however the import I'm doing already has this method. How can I call a local class?
I've tried this:
mypackage.subpackage.JSON.encode(param1)
In C # the above would work, but I do not know why in ActionScript below does not work.
import flash.utils.ByteArray;
public class Package extends ByteArray
{
public function writeJsonObject(param1:Object) : void
{
this.writeUTF(JSON.encode(param1));
}
}
I get this error:
Error: 1061: Call to a possibly undefined method decode through a reference with static type Class
Try using JSON.stringify()
import flash.utils.ByteArray;
public class MyClass extends ByteArray
{
public function writeJsonObject(param1:Object) : void
{
this.writeUTF(JSON.stringify(param1));
}
}
Reference: https://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/JSON.html
Also worth noting the difference between writeUTF and writeUTFBytes to understand exactly what's getting written to your ByteArray
I moved my code from my Application to a separate AS class file and now I'm getting the following errors:
1061: Call to a possibly undefined method addEventListener through a reference with static type Class.
1180: Call to a possibly undefined method addEventListener.
.
package managers {
import flash.events.Event;
import flash.events.EventDispatcher;
public class RemoteManager extends EventDispatcher {
public function RemoteManager() {
}
public static function init(clearCache:Boolean = false):void {
addEventListener(SETTINGS_CHANGE, settingChangeHandler);
addEventListener(ITEMS_UPDATED, itemsUpdatedHandler);
}
}
}
Your code
addEventListener(SETTINGS_CHANGE, settingChangeHandler);
evaluates to
this.addEventListener(SETTINGS_CHANGE, settingChangeHandler);
There is no this in a static method, since it's designed to function without an instance. Furthermore, you cannot attach an event listener and dispatch events from a static class.
Either change your function declaration to
public function init(clearCache:Boolean = false):void
Or implement a singleton pattern to kinda get a "static class, that dispatches events".
Singleton with event management.
You have declared your method init as static , so all you can use into this one is static field, static method, no object that belong to the instance.
Remove the static from the function or try to implements a singleton if it what you are after.
here a quick really simple one :
public class RemoteManager extends EventDispatcher {
private static var _instance:RemoteManager;
public function static getInstance():RemoteManager{
if (_instance == null) _instance=new RemoteManager();
return _instance;
}
public function RemoteManager() {
if (_instance != null) throw new Error("use getInstance");
}
public static function init(clearCache:Boolean = false):void {
getInstance().addEventListener(SETTINGS_CHANGE, settingChangeHandler);
getInstance().addEventListener(ITEMS_UPDATED, itemsUpdatedHandler);
}
}
// use it
RemoteManager.init();
Does anyone know of a framework, preferably some way to have the Flex compiler run an extension or perhaps just a build step that we could generate strongly typed proxy classes of our application's data models.
There are 2 main things we want to do with the proxy's:
At runtime we want to lazily parse and instantiate the instance as accessed (similiar to how Java's Hibernate has Lazy proxy objects)
In an editor application we want to implement setter calls so we can track which objects have been modified
The Proxy is really necessary in this situation beyond things like programatically setting up ChangeWatcther's because we need to track Array adds/remove and possibly track "reference" objects so that when a "reference key" is changed we know to save those objects that are referencing it by key
In the first case we want the proxy to basically abstract when that object is loaded from serialized data, but still pass around references of it with the same public properties and data access pattern if it were the real object.
Basically the proxy would instantiate the object the first time a method is called on it.
I know we could use some AS3 byte-code libraries like as3-commons-bytecode.
Or possibly repurposing the GraniteDS Code Generation.
I'd prefer to generate code because it is a deterministic thing and it'd be nice if we could have a way to debug it at runtime easier.
Does anyone know if I could do something like MXMLC does when it generates AS3 code from MXML files.
Also is there anyway to control "when" in the compilation pipeline I can generate code, because we have a lot of data objects using public fields instead of getter/setters, but that are [Bindable] and so if I could generate the proxy based on the generated getter/setter methods that would work.
Here's an example application data object and proxy classes:
[Bindable]
public class PersonDTO implements Serializable {
private var _name:String;
private var _age:Number
public function get age():Number {
return _age;
}
public function set age(a:Number):void {
_age = a;
}
public function get name():String {
return _name;
}
public function set name(n:String):void {
_name = n;
}
public void readObject(data:*) {
//...
}
}
// GENERATED CLASS BASED ON PersonDTO
public class LazyProxy_PersonDTO extends PersonDTO {
private var _instance:PersonDTO = null;
private var _instanceData:*;
private function getInstance():void {
if (_instance == null) {
_instance = new PersonDTO();
_instance.readObject(_instanceData);
}
}
override public function get age():Number {
//Ensure object is instantiated
return getInstance().age;
}
override public function get name():String {
//Ensure object is instantiated
return getInstance().name;
}
}
// GENERATED CLASS BASED ON PersonDTO
public class LogChangeProxy_PersonDTO extends PersonDTO {
//This will be set in the application
public var instance:PersonDTO;
//set by application
public var dirtyWatcher:DirtyWatcherManager;
override public function set age(a:Number):void {
dirtyWatcher.markAsDirty(instance);
instance.age = a;
}
}
Digging a little deeper into AS3-Commons byte code library it looks like they support generating proxy classes and interceptors.
http://www.as3commons.org/as3-commons-bytecode/proxy.html
public class DirtyUpdateInterceptor implements IInterceptor {
public function DirtyUpdateInterceptor() {
super();
}
public function intercept(invocation:IMethodInvocation):void {
if (invocation.kind === MethodInvocationKind.SETTER) {
if (invocation.arguments[0] != invocation.instance[invocation.targetMember]) {
invocation.instance.isDirty = true;
}
}
}
}
I read a question on stackoverflow (couldn't find it now) about how variables in a method can be only accessed in that method, but the code still works with the answer being an analogy of a hotel room. In AS3, I believe everything that's not primitive gets passed as a reference. So, the following code would be the same as that question and isn't guaranteed to work?
public class Testy {
private var foo:Array;
public function Testy(input:Array) {
// Allow the whole class to access it
foo = input;
}
public function traceFoo(){
trace(foo);
}
}
Now, foo would be a reference to the input argument in the class' constructor. Is this safe code/good practice? Thanks!
Yes this is safe/good code practice as long as you don't want to manipulate the original Array. If you want to manipulate the original array, allow public access to the array by making it a public var or using a public getter/setter.
What you've described is a property, and is inline with encapsulation of object oriented programming.
This would expose a getter and setter:
package
{
import flash.display.Sprite;
public class Testy extends Sprite
{
private var _foo:Array;
public function get foo():Array
{
return _foo;
}
public function set foo(value:Array):void
{
_foo = value;
}
public function Testy()
{
super();
}
}
}
Also it's better to return _foo.concat() in getter not to break encapsulation.
How can I make an abstract class in AS3 nicely?
I've tried this:
public class AnAbstractClass
{
public function toBeImplemented():void
{
throw new NotImplementedError(); // I've created this error
}
}
public class AnConcreteClass extends AnAbstractClass
{
override public function toBeImplemented():void
{
// implementation...
}
}
But.. I don't like this way. And doesn't have compile time errors.
abstract classes are not supported by actionscript 3. see http://joshblog.net/2007/08/19/enforcing-abstract-classes-at-runtime-in-actionscript-3/
the above reference also provides a kind of hackish workaround to create abstract classes in as3.
Edit
also see http://www.kirupa.com/forum/showpost.php?s=a765fcf791afe46c5cf4c26509925cf7&p=1892533&postcount=70
Edit 2 (In response to comment)
Unfortunately, you're stuck with the runtime error. One alternative would be to have a protected constructor.... except as3 doesn't allow that either. See http://www.berniecode.com/blog/2007/11/28/proper-private-constructors-for-actionscript-30/ and http://gorillajawn.com/wordpress/2007/05/21/actionscript-3-%E2%80%93-no-private-constructor/.
You may Also find these useful: http://www.as3dp.com/category/abstract-classes/ and, in particular, http://www.as3dp.com/2009/04/07/design-pattern-principles-for-actionscript-30-the-dependency-inversion-principle/
package
{
import flash.errors.IllegalOperationError;
import flash.utils.getDefinitionByName;
import flash.utils.getQualifiedClassName;
import flash.utils.getQualifiedSuperclassName;
public class AbstractClass
{
public function AbstractClass()
{
inspectAbstract();
}
private function inspectAbstract():void
{
var className : String = getQualifiedClassName(this);
if (getDefinitionByName(className) == AbstractClass )
{
throw new ArgumentError(
getQualifiedClassName(this) + "Class can not be instantiated.");
}
}
public function foo():void
{
throw new IllegalOperationError("Must override Concreate Class");
}
}
}
package
{
public class ConcreteClass extends AbstractClass
{
public function ConcreteClass()
{
super();
}
override public function foo() : void
{
trace("Implemented");
}
}
}
In AS3 would just use interfaces to make sure all functions are implemented at compile time.
I know it different but does the trick for an example such as the one above.
As long as they don't permit non-public constructors in actionscript, you'd have to rely on run time errors for abstract classes and singletons.