[Clug-tech] JavaScript objects and inheritance
John Jardine
john_e_jardine at spamcop.net
Thu Sep 4 01:29:34 PDT 2008
Thanks for the pointer to JSON. I'm going to pursue two approaches: 1)
Directly access Java objects from JavaScript & ActionScript and (2)
passing them back and forth with JSON. This may well come down to which
approach (works/is easier) for my partner.
On Wed, 2008-09-03 at 12:49 -0600, Shawn wrote:
> Sounds to me like you are looking for JSON.
>
> JSON is just a string representation of an object.
>
> So, using your generic Java Message you might send the following string:
>
> { "msg_version": "value1", "msg_userid": "value2", "msg_session": "value3", "msg_class": "value4", "msg_type": "value5" }
>
> This "object" would be the myMessage object. Of course that can be extended
> as needed. You can pass arrays, objects, or even methods this way. That
> string is simple javascript so can be converted back to an object
> quickly/easily. There are safe and unsafe ways of doing this, at one extreme
> is just using an eval statement
>
> var myMessage = eval(json_string);
>
> This isn't considered "safe" as it can easily introduce script injection
> issues. But it does work fine, and is suitable if the json_string is from a
> trusted source. There are other/safer methods - a little googling for JSON
> will lead you to them. (don't know them off the top of my head - I usually
> just use the jQuery library for this)
>
> So, for your specific problem, you might have something on the client side
> that looks like this:
>
> function dataArrives(data_string) {
> var myMessage = eval(dataString);
> switch (myMessage.msg_type) {
> case "login": doLogin(myMessage.login_name, myMessage.password); break;
> //etc.
> }
> }
>
> As for "extending" the core object when needed, that is a simple case of
> setting or adding other properties when needed. In JS there is nothing
> special needed here.
>
> var myMessage = standardMessageObject;
> myMessage.johnsName = "John";
>
> Converting your object to string can also be done via code:
>
> function toJson(target) {
> var out = "{";
> for (p in target) {
> out += "\"" + p + "\": \"" + target[p] + "\", ";
> }
> out += "}";
> return out;
> }
>
> There are more robust functions available for this on the web. That is just
> off the top of my head.
>
> This is totally client side. Most server side languages have a method for
> converting an existing object to a JSON string, and/or creating an object
> from a JSON string. And because it is simple string manipulation, processing
> overhead is very minimal - depending on the complexity/volume of your data.
>
> Oh, Actionscript is just a variant of ECMAScript (aka JavaScript). So all
> this should work just fine in flash, or in the browser.
>
> here's some links to help get you started.
>
> http://www.json.org/
> http://www.google.ca/search?q=json&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a
>
> HTH (and hope I haven't completely missed your need here..)
>
> Shawn
>
>
> On Wednesday 03 September 2008 11:12:36 John Jardine wrote:
> > I was wondering if there is an easy way to this...
> > I am sending messages between a server and a flash file running in a
> > browser.
> >
> > The browser is using a Java applet to send/receive the data because it's
> > UDP based. The messages all start out the same (header info) but differ
> > 16 bytes in, based on the type of data they'll carry. I'd like to be
> > able to do it via a somewhat generic object.
> >
> > So to summarize the current data flow:
> > Internet UDP packets <=> Java <=> JavaScript <=> ActionScript
> >
> > In a perfect world I'd like to have ActionScript send/rcv UDP
> > packets ... but it's not a perfect world. This has led me to the
> > current method. Now I'm trying to make the client message handling more
> > generic and a little less fragile.
> >
> > What I'd like to end up with is a generic Java message:
> > public class myMessage {
> > public int msg_version;
> > public int msg_userid;
> > public int msg_session;
> > public int msg_class;
> > public int msg_type;
> > .
> > .
> > .
> > Then extend this generic class with more specific message types:
> > public class myLoginMessage extends myMessage {
> > public int msg_version;
> > public int msg_userid;
> > public int msg_session;
> > public int msg_class;
> > public int msg_type;
> > public String login_name;
> > public String password;
> > .
> > .
> > .
> >
> > Then as these message types arrive, I have Java call JavaScript which
> > retrieves the message and passes it on to ActionScript.
> >
> > Is there a way to do this or am I stuck creating different objects for
> > each message type?
> >
> > Any insight's would be welcome. I already have something working - I
> > just don't like it and am looking for something better.
> >
> > Another thing... the whole communications design is to off-load the
> > server as much as possible. I don't care how hard or convoluted the
> > client side gets as long as I can keep my server side ridiculously
> > simple (and blazingly fast).
> >
> >
> > _______________________________________________
> > clug-tech mailing list
> > clug-tech at clug.ca
> > http://clug.ca/mailman/listinfo/clug-tech_clug.ca
>
>
>
> _______________________________________________
> clug-tech mailing list
> clug-tech at clug.ca
> http://clug.ca/mailman/listinfo/clug-tech_clug.ca
More information about the clug-tech
mailing list