Posts tagged as3
QuickTip: asSQL Connection Problem with non-localhost hostname
Dec 12th
asSQL is just great. It’s an ActionScript 3.0 library for connecting AIR applications to MySQL databases and it works brilliantly – when it works! Recently i gave it a shot again after an utter failure the last time i tried it… The library seems to be a port of the Java implementation of connecting to MySQL (Connections, Statements, Fields, etc). It takes a little getting used to but it’s an excellent library and i highly recommend it.
Background
My development environment is Windows-based (against my wishes… not enough loot or motivation for a Mac, lack of Adobe software on Linux) but i make up for this by running a virtual Linux installation in my Windows environment and networking the two together. It actually works really well, and the process for setting this up can be found here. To cut a very long and boring story short, essentially i cannot use localhost as your server when using this architecture since i have my LAMPP stack running on my Linux environment, so my “local server” can only be accessed using the IP address of the virtual system.
This complicates things a bit when trying to use asSQL to connect to my MySQL installation, because MySQL gets all paranoid and won’t accept connections from a foreign IP address (the IP of my Windows machine on which my AIR app is running)… To get around this, all you have to do is create a new user in MySQL (i used phpMyAdmin), set the user’s hostname to the IP address of your remote system (i’ve explained how to do this below) and you’re ready to rock and roll!
Solution
The first thing you need to do is get the IP address of the remote system…
Windows: Open the command line, type ipconfig
Linux & Mac: Open the terminal, type ifconfig
In this example, my IP address is 192.168.56.1
I’ll be using phpMyAdmin to fix the problem.
- Open phpMyAdmin and click on the Privileges tab.
- Click Add a new User
- Enter any username you like, paste the IP address obtained in the Host field and put in a password
- Under the Database for user panel, leave it at None
- Under the Global privileges, you can Check All
That should sort it out… Remember, this is HIGHLY insecure and should only be used on development environments and NOT production environments.
Union Platform – Part I (Installation)
Jul 8th
We’ve all heard about Colin Moock at some point in our Flash/Flex development career. Colin has authored a great many ActionScript books throughout the years and as a consequence has helped thousands – including myself – to learn ActionScript. Now Colin has hit the community with something incredible… the Union Platform.
Here is the official definition of the Union Platform from http://www.unionplatform.com: “Union is a development platform for creating multiuser applications. It includes the Union Server, a multiuser communications server, and Reactor, a framework for creating Adobe Flash client applications.”
Essentially what this means is… you can now build realtime chat applications, MMORPG games, and anything else that could use a little realtime flavour
Installation
In this part of the series on the Union Platform i will be showing you how to install the Union server on your *nix machines and Window$ machines. Let’s leave the hideous beast (Window$) for last, and begin with a real operating system that is actually a pleasure to work with, and doesn’t make you praise whichever god you worship that your legs can’t bend backwards at the knees to kick yourself in the balls for ever getting roped into that shitty OS.
I’ll be Linux Mint for my *nix installation explanation, and this will work on Macs too in much the same way.
The Union Platform Server is a Java application that will run on your server and listen on port 9100 and 9101 for traffic. Be sure to check that your firewall doesn’t block this port before you begin…
First things first, head over to the downloads page on the Union site and download the GZipped tarball to your desktop.
Next, unzip the tarball’s contents (tar -xf union_1.0.0_alpha2.tar.gz). This will create a folder called union on your desktop. Now, move either the folder in its entireity or the contents to a folder somewhere on your machine.
Perfect. So now we have all the files we need. Make sure that you have Java installed and working correctly on your machine or otherwise this will not work. You will need to make sure that this folder had execute permissions, so issue the command chmod 775 * to give all the files the necessary permissions.
Usage
Once this is done, we’re done! To start the Union Platform Server issue this command: ./startserver.sh from inside the folder you unzipped. You will now see your terminal bombarded with debug messages notifying you of Union‘s successful start.
Window$ users please note: the instructions above are pretty much the same for Window$; merely extract the files to your machine and run the batch file startserver.bat.
Administration Panel
Colin Moock and crew have been generous enough to provide us with a Flex administration panel for the server, and you can find it here. Download the SWF, and open it in your browser. You will be met with a login screen like this:
NOTE: If you look in the union.xml configuration file, you will find the password that is used by the server. By default it is password. Also note that the administration panel runs on port 9110.
Once you have logged in, you will see a screen like this:
Pretty bloody cool if you ask me!
Join me in the next part of this series when i will be demonstrating how to build a simple realtime chat application!
Making your constructors more useful
Mar 30th
The constructor function in Object-Oriented languages is an incredibly useful mechanism to use, and we’ve all used them for a wide variety of solutions.
When i use PHP with Flex, i like taking advantage of AMFPHP’s amazingly nifty feature of being able to send whole objects as binary data to and from the server. This can be vastly useful because you could send a whole object from Flex as an ActionScript class and PHP will receive it and use it as the same class! Here’s a practical example:
You are making an application to control a university’s student details. One logical step would be to go and create a Student class in ActionScript 3.0:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package { public class Student { public var ID:uint; public var firstName:String; public var surname:String; public var age:uint; public var accountPaid:Boolean; public function Student() { } } } |
Now, if you’re using AMFPHP, it’d be a complete ball-ache to have to send the Student class like this (say you were inserting a student into the database):
var student:Student = new Student(); student.firstName = "Jacob"; student.surname = "Zuma"; student.age = 67; student.accountPaid = false; service.addStudent(student.firstName, student.surname, student.age, student.accountPaid);
Wouldn’t it be so much easier to simply do this:
service.addStudent(student);
…and receive the object in PHP, and serialize it into this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?php class Student { public $ID; public $firstName; public $surname; public $age; public $accountPaid; public function __construct() { } } ?> |
Well, it’s possible! I covered this is some detail in one of my previous tutorials, and Flex-to-PHP remoting is not in the scope of this particular post. The focus of this post, however, is to give you a couple of tips as to making your constructors more intelligent in the realm of Flex remoting. My two main areas of web development are ActionScript 3.0 and PHP5, but you can easily apply the same logic that follows to all other Object-Oriented languages.
Below is the improvement on my earlier Student class, with the more remoting-friendly constructor:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | package { import flash.utils.describeType; public class Student { public var ID:uint; public var firstName:String; public var surname:String; public var age:uint; public var accountPaid:Boolean; public function Student(values:Object=null) { if(values) { var props:XMLList = describeType(this)..accessor; for each(var prop:XML in props) this[prop.@name] = values[prop.@name]; } } } } |
* More information on the describeType function here.
And the PHP equivalent:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?php class Student { public $ID; public $firstName; public $surname; public $age; public $accountPaid; public function __construct($properties=null) { if($properties) { $properties = new ArrayObject($properties); foreach(get_object_vars($properties) as $prop => $value) $this->$prop = $value; } } } ?> |
Now, you can easily pass an object (with no type) to either of these constructors, and they will automatically transform that object into a strongly typed, meaningful and above all else – easy to work with – variable.
Succinct Singletons
Feb 11th
A singleton can be a very useful and essential Object-Oriented device in many instances (no pun intended). Singleton classes only allow one instance of that particular class, and this can be very useful in situations where you want one object to be used throughout your Flash/Flex application instead of many separate ones. I usually use singleton classes for classes that merely act as utility proxies to my applications.
Below is the code to create a very succinct singleton
package { public class Singleton { private static var _instance:Singleton; public static function getInstance():Singleton { if(!_instance) _instance = new Singleton(); return _instance; } } }
In the upcoming API i’m planning on releasing, i’ll be using Singletons extensively, and the API will – of course – be open source.
Working with the Security and SecurityPanel classes
Feb 5th
In a recent project, i was asked to build a system that would support storing cookies on a user’s local machine. This is obviously solved with SharedObjects if you’re working with the Flash Player. However, i ran into a few issues with this and found out a few interesting things.
Firstly, i wanted to detect if the user allows Flash/Flex to write to their computer. This can be solved as follows:
You can use the flush function of the SharedObject class… mySharedObject.flush(1) where 1 is the number in bytes that you require for your SharedObject. If the function returns a value of SharedObjectFlushStatus.PENDING, there is not sufficient space to store your SharedObject and a dialog appears that prompts the user to rectify this. If the function returns false, however, the user has not permitted using SharedObjects.
To call up a user’s Flash settings dialog (Camera, Display, Storage, Microphone, Privacy, Settings Manager), you would use the Security class from the flash.system package.




