Has anyone considered openFrameworks to Flash communications via binary data on an AS3 socket? This is compared to traditional OSC to Flash via FLOSC (UDP to TCP).
Read More: http://www.bytearray.org/?p=64
The AS3 would look something like:
//n_socket.addEventListener (ProgressEvent.SOCKET_DATA, onSocketData);
private static function onSocketData( e:Event ):void
// grab the bytes coming in
while ( e.target.bytesAvailable > 0 )
coordsBytes.writeByte ( e.target.readByte() );
// when a full packet is available, parse it
if ( coordsBytes.position == streamLength )
inputs.position = 0;
inputs.writeBytes ( coordsBytes );
coordsBytes.position = inputs.position = 0;
parseBytes ( inputs );
But I need some help on how to do it with OF… anyone help???
wow binary sockets and flash is great news - since when I was doing OF->flash stuff, we had to use the xml socket and base64 encode the data (like how you get binary data in an email attachment). it’s was a HUGE pain.
what exactly do you need help with? For diego I’m looking at binary OF->OF communication over udp right now and this could be helpful. I should be able to post shortly.
I’d first try very simple things, like:
a) sending one byte on mousePressed to flash
b) sending one byte from flash to OF also on an even, like a button press
c) trying larger amount of bytes
then, you could work in a call and response model, like this:
flashApp: hey C++ app, I am ready for data !
cppAp: ok, cool, here you go: “2389rpfhasbc23asdf98vn9834f”
call and response will help alot since flash will be going at a different clock speed the c++, (likely slower) and if c++ is sending data all the time, it might not read fast enough.
hope that helps - I don’t code in AS3 but the udp -> binary stuff I’m hacking now might be helpful, I’ll try to post up in a sec.
Not completely sure, but I was trying that some time ago and I think that although as3 has a binary socket it doesn’t support udp, only tcp. So you’ll yet need a proxy to comunicate with osc.
oh good call - forgot about that
I think the question (if I’m not mistaken) is not about using OSC but about doing straight OF -> flash in binary format. which is totally possible – with the OF network addon theo put together (and based on christian nagelhofer’s code) it’s not that har to switch between udp or tcp. I will try to put something together shortly .
I tried this last week and it worked very smooth
I sent an array of objects, each containing two ints and one float, with the of tcp server class
myOfTcpServer.sendRawBytesToAll((char*) myDataArray, nElements * sizeof(myDataObject));
on the flash side i receive it like
private function handleSocketData(a_event:ProgressEvent):void
you also need to set the socket to use little endian (big endian by default)
private function initSocket(host:String, port:int):void
mySocket:Socket = new Socket(host, port);
mySocket.endian = flash.utils.Endian.LITTLE_ENDIAN;
Hope that helps
Thanks Henrik, I’ll try this out… quick question… could you post how you got your data into that array? also explain what nElements and myDataObject is?
MyDataObject is a class, which in my case only has two ints and one float, no functions.
myDataArray is an array containing instances of the class MyDataObject
nElements are the number of instances in the array.
( nElements* sizeof(MyDataObject) = how many bytes to send )
Here is a link to a blog from a friend with a example a flash binary socket (flash-binary-socket-client-c-sharp-socket-server)
To emulate a Flash client like the TCPClient example (so that sends a text message to the server), would you use:
It doesn’t seem to work.
The Flash client can get the response from the server, but I still can’t make the server receive what Flash i sending. Any clue?
I’m trying to send an object containing 2 int’s and 1 float.
The size of its instance seems to be 4 bytes. In Flash the size of what I get back is usually 4 bytes, but sometimes 8 or 12. Is it because it’s receiving two or three instances at the same time? Btw, in Flash I’m getting errors, because I presume Flash is expecing 32bit ints. when probably the one is receiving are 8bit, right?
Which approach would you recommend me? Do I have to use SInt32? I tried and the size doesn’t seem to change. Where is the catch?
It’s hard to say what your problem is without knowing how you send and read the data. But both an int and a float is 4 bytes and that’s what flash is expecting.
I’ve put my own test online. There are two zips, a flex project and an OF project for xCode. The OF app is a motion detection server that sends an array of objects containing two ints and one float,(x, y & val). The flash app just traces the values.
I stopped working on this as soon as i got the connection working but it should get you started.
i’ll leave it up for a few days.
There is something wrong here. If I check the sizeof an attribute of my model is still 4 bytes. So I presume that if I do sizeof(model) I’m not getting the sum of each attribute
I’m wondering if there is an elegant solution to transform my instance to bytes like henrik did than building the array by hand.
the reason why the first case doesn’t work is because model->x is a value, or what to call it. The first argument to sendRawBytes() must be a pointer to value not the value it self. so try somthing like
TCP.sendRawBytesToAll( (char*) *(model->x), 4);
i.e the sendRawBytes function need a pointer that tells it where in the memory it should start to read.
Hi Henrik, thanks for the help.
With your indication I get:
error: invalid type argument of ‘unary *’
My mistake was on calculating the size of my instance. Thanks to a colleague the size is retrieved with:
and to get the bytes array a normal casting to (char*).
It would be pretty cool to use AMF though
Could anyone post a zip file of OF talking with Flash via binary? Something just sending mouse coords would be a great start…
I wonder how much performance gain we get using binary…
You can see an explanation here: http://blogs.conchango.com/filthyria/ar-…-flash.aspx
I will see if I can wrap it up in a zip file.