I have two software to create with AIR: a server and a client. Both with AS3 and AIR.
I want to avoid having a big architecture with Remote Shared Object, or using BlazeDS, or having to set a Java / Python / ... server in the m开发者_开发问答iddle.
In order to avoid having to parse socket data in the server and in the client side, I thought about using XMLSocket on the client side: the data arrives in a way which is already parsed by the Flash framework.
However, when I use this XMLSocket with the AIR ServerSocket, the "connect" event returns me a raw Socket on the server side. So I have to manually manage the XML data on the server side.
Is there a way to also use an XMLSocket on the server side to communicate with the client? A kind of trick to convert the returned Socket into an XMLSocket?
If there is an AS3 library which manages that, I did not find it.
Thank you!
I totally agree with Adrian in that server side code in ActionScript is going to be bad, Java being syntactically not that far would be much much better: hotspot JIT, multithreaded, with lots of highly optimized server side components.
Now having said that: XML Socket presents you with a parsed XML, still what is going to be transmitted over the wire will be just XML as text. So performance-wise it is just the same as using a regular socket and parsing yourself. And this can be as easy as:
var text: Object = <data received>
var xml: XML = new XML(text);
So once you've received a full xml payload you're ready to go.
And if you think XMLSocket can take care of getting a full XML before parsing, I did some XML Socket stuff and got XML delivered in two or more fragments and had to cope with it myself, her an example:
...
private var xmlData: String = "";
private var errorCount: int;
...
private function dataHandler(e: DataEvent): void {
if (errorCount > 2) {
errorCount = 0;
xmlData = "";
}
try {
xmlData += e.data;
processMessage(XML(xmlData));
// valid XML, clear the buffer
xmlData = "";
} catch (error: Error) {
// otherwise log error and wait for the rest of the XML
log.error(error.toString());
log.error(e.data);
errorCount++
}
}
I can't say for certain about server-side, but I would imagine AS3 is a poor language for a server (simply due to lack of multithreaded support). Regardless you might try casting it to a new object:
var myXMLSocket:XMLSocket = myOldRegularSocket;
At the very least if it throws an error, the error might give you some insight. I advise against XML sockets, as they try to mask the socket, but really only convolute it. If you only needed to send XML data, you could do so in an near-identical 'Socket' structure.
It seems AIR contains a 'ServerSocket' object type. This is probably your best bet.
Take the extra bit of time to learn how 'raw' sockets work, it will save you converting data all over the place. Regular expressions are much faster for parsing through data than xml, especially if the data is always dynamic.
精彩评论