Skip to main content
December 4, 2006
Question

Architecture of basic app with no components

  • December 4, 2006
  • 5 replies
  • 363 views
I'm coming from an extensive background in AS1.0 and 2.0, and some level of 3.0. I consider myself fluent inI'm coming from an extensive background in AS1.0 and 2.0, and some level of 3.0. However, I'm having a surprisingly hard time understanding how a basic flashcom app works. It seems most of the information I've found relies on the use of components, which I want to avoid as much as possible. If someone could point in the right direction, or give me the basic architecture, that would be wonderful.

Some slightly more specific questions:
- Do remote SharedObject writes get broadcasted to all connections?
- What is the role of server-side ActionScript? It seems to me that after accepting/rejecting the connection almost everything could be scripted client-side, with writing/reading to various remote SharedObjects. However it looks like all the components have extensive server-side AS.

To give a practical example, how would I construct a very basic chat? I know everything about how to create a Flash interface to do this, and in fact have worked on messaging apps where I was sending information via LoadVars to a database, and the database gurus were handling the rest. But how would I connect users, and have their messages broadcast and read by all other connected users?

Thanks for any tips, I know this is a painfully broad question, but I'm just having a hard to getting kick started as I sift through the mosterous pile of component code...
    This topic has been closed for replies.

    5 replies

    December 5, 2006
    Great, thanks.

    1. Data gets lost. So, the solution is to not let multiple users write to the same property. So what would I do in the case that I *want* users to write to the same property? In the drawing app, I can't think of a legitimate reason, as I can always merge multiple users drawing data on the client side, but what about an example such as game where users kick around a ball. There would be only one ball with an x,y, and all users would somehow have to modify the ball x,y.

    2. Too much data. So, my sturucture was like this:
    SO -> drawingN:Array - pointN:Array
    In the suggested alternative, the drawing data is not divided, so all drawing data for all drawings would be in the same Array... I guess that's not really a problem, I'd just add another property to define what drawing the point belongs to.

    BTW, I keep reading about "slots". As far as I can tell from context and description, it seems like a "slot" is just an Array. What is the difference? Is a "slot" just the term for a SO property, which is really functionally like an Array?
    December 5, 2006
    You have two problems:

    1. Data gets lost.
    2. Two much data is being transferred.

    The lost data is due to the fact that all clients are rewriting the same data at the same time. One solution to this is to hav a unique id for each client and let everyone write to his own property on the so:
    clientId = 1;
    ...
    so[clientId + "_drawing"] = [...

    The second problem is due to the fact that you are rewriting an entire array of objects each and everytime a point is updated. If you separate each instruction into a unique property on the SO, that's the only data that will be transferred instead of the whole chunk:
    clientId = 1;
    drawingInstructionCounter=0;
    ...
    so[clientId + "_" + (drawingInstructionCounter++) + "_drawing"] = {action:'lineTo', x:25, y:45, color:0xff0000};

    This has the added benefit of you not having to keep track of what's allready updated. The only data in the onSync event is new data.

    To finally streamline it some more, try to keep the transferred data to a minimum at all times:

    clientId = 1;
    drawingInstructionCounter=0;
    ...
    so[clientId + "_" + (drawingInstructionCounter++) + "_d"] = {a:'lt', x:25, y:45, c:0xff0000};

    /Johan
    Man Machine
    December 5, 2006
    Thanks again, both, I've put together a drawing app with sharred cursors and what-not, using no components. I think I just fell in love with FMS.

    However, the lag on the drawing part is something awful. We are using a low budget account from Influxis--I don't have the specs but I want to say something like 10 connections and low bandwidth, so I'm sure that plays a role. However, the cursors seem to broadcast and update fine, it's just the drawing.

    How I'm doing the drawing is using a single SO, "drawings_so", which contains named drawings, like "myDrawing","anotherSketch", etc. Those are Arrays which contain a brutally long list of drawing point data, like this:
    [
    {action:'lineTo', x:25, y:45, color:0xff0000},
    {action:'lineTo', x:45, y:15, color:0xff0000},
    {action:'moveTo', x:255, y:455, color:0xff0000},
    etc.
    ]

    A new point object is generated onMouseMove(when the user is drawing), so the data gets big very fast. I'm using a simple onSync event, and it gives me the drawing object, ex "myDrawing" or "anotherSketch" after they change, which conains the entire drawing in stored points like above. I'm doing some simple tracking in the client so that it doesn't have to redraw the entire thing, only the new stuff, but the lag between SO onSync updates is quite large. I've also noticed that if more than one user is writing to the same drawing(ex. "myDrawing" in drawings_so), data gets lost and out of sync.

    Any comments on this architecture? Any tips on speeding things up or doing things smarter? Right now, for the sake of testing, I'm not doing anything server-side, but would move that direction.

    It seems as though writing/reading from a SharedObject is not the most efficient way when doing such constant updates between multiple clients, can NetStream replace that functionality in this case? The problem seems to be in the the loadtime of the SharedObject, as if the clients have to completely reload the remote SO when its updated.

    Thanks in advance!
    December 4, 2006
    NetConnection
    NetStream
    SharedObject
    Video

    That's about all you need.

    SharedObjects propagate their changes to all clients who have connected to them.

    To create a chat, you can either set up a SharedObject as a chat room and have all clients read and write the messages from/to the SO or you can write some server side code (there it is!) that recieves a NetStream (which you can write allmost like a normal Stream) and broadcast it to all recipients.

    /Johan
    http://www.man-machine.se
    December 4, 2006
    Thanks, Jay and Machine, for taking the time to respond to such a general question. This is quite helpful.

    I was eyeing O'Reilly's book, so its good to hear it recommended. Also I found that there are livedocs on FCS/FMS, which looks very helpful.

    > NetConnection
    > NetStream
    > SharedObject
    > Video

    Well, that's good, because I already know how to use all those classes on the client SWF(at least progressively, not in context to FCS/FMS).

    I think at this point I could actually make a text chat. Basically I just need 1 persistent(or temporary) remote SO which logged in users append text to, either by writing to the SO or sending info and having SSAS choose to write to the SO, like you said, Jay, which sounds smarter. Then the changes are grabbed by the onStatus and written the the text history textfield. Question is, would I append each message as a new property(in an array, say) on the SO, so that the entire thing doesn't have to be reloaded every time? Or simply have a single property "text" which when changed gets re-assigned to the text history textfield in the client's SWF?

    Now how about using NetStream, that sounds interesting... any tips to get started in the right direction there? I've only ever used that with Video, how do I publish/play text data?
    December 4, 2006
    For starters, you might want to pick up a copy of "Programming Flash Communication Server" (O'Reilly). It's a very comprehensive guide, and all of the info in there is applicable to FMS. It will teach you everything you need to know to get away from the prefab communication components (It will also teach you how to use the component framework to build your own components, but you don't have to do that if you don't want to)

    When a shared object is updated (either on the client side or the server side), the update is automatically broadcast to all of the clients... assuming the sharedObject hasn't been locked (you can programatically lock/unlock so's). The sharedObject class has an onStatus method, which is invoked every time there is a change in a property of the shared object. Both the client and the server can have onStatus handlers for any shared object.

    The role of SSAS is pretty far reaching. Quite honestly, I prefer to have the server handle writing/updating so's rather than allowing it to happen on the client side. I could go on all day explaining why, but the short answer to it is having a single point of control over the shared data. If only the server writes SO properties, you can more easily control conflicts and collisions.

    The chat example is a little much to type here. I'll see if I can find a link to an existing tutorial.