Flash with HTTP Client Lib and Compojure

I just managed get communication between Compojure and Flash (AS3) up and running, by way of HTTP. No big deal really when all the dependencies and whatnot are in place. I did not use URLRequest or HTTPService, but AS3HttpClientLib as I got the impression it seems to be more feature complete than the other two that seem more basic and default. An example of a feature I might want to use in the future is for instance the HTTPS stuff.

Why not use AMF then with something like BlazeDS you might ask and the answer is that I want to avoid these convoluted things that will require even more boiler plate bullshit. JSON over HTTP is good enough for me for the time being. It will be a dear problem if I someday have to consider AMF due to massive traffic.

Let’s get on with the actual demo. Start with downloading AS3CoreLib and the aforementioned AS3HttpClientLib, link both swc files by going to something like project settings -> ActionScript 3 -> libraries and add the swc files directly. At first I simply linked to the folder where I put them but Flash complained on missing dependencies until I linked to them separately and directly.

First the AS3 which I simply put in the first frame of my Flash CS4 project:

import flash.net.*;
import com.adobe.net.*;
import org.httpclient.*;
import org.httpclient.http.*;
import org.httpclient.events.*;

var client:HttpClient = new HttpClient();

var uri:URI = new URI("");
var variables:Array = [{name:"fname", value:"FirstName1"}, {name:"lname", value: "LastName1"}];

client.listener.onData = function(event:HttpDataEvent):void {
  var stringData:String = event.readUTFBytes();

client.postFormData(uri, variables);

This is basically just a combo of the POST and GET demos on the AS3HttpClientLib homepage, note though that we’re using POST. I don’t like the fact that you need to use a whole object for each key and value in the post, it should’ve been a big object instead with names as keys, that is probably something I will look into changing if I get serious with the Flash and Compojure interop.

Anyway, we need a Compojure server too:

(ns hello-world
  (:use compojure.core

(defroutes main-routes
  (POST "/hello" [fname lname]
    {:status 200, :body (str "Hello " fname " " lname)})
  (ANY "*" []
    {:status 404, :body "<h1>Page not found</h1>"}))

(run-jetty main-routes {:port 8080})

Note how we receive the two values by name that we post from Flash above, in flash we will of course trace “Hello FirstName1 LastName1”.

And that was that, with the help of the prior tutorial you should now be able to manipulate a Cassandra database with Compojure as the server and using Flash as the GUI.

Related Posts

Tags: , ,