Sending OSC
Besides receiving Tuio tracking data the tuio-as3 library is also capable of sending osc messages. This could be very interesting for osc based communication between several flash movies or if you want to flash as a ui framwork only and e.g. sound synthesis is done by a another program which receives commands from the flash movie via osc.
To send osc messages you have to create an OSCManager.
-
import org.tuio.osc.*;
-
import org.tuio.connectors.*;
-
-
var oscManager:OSCManager = new OSCManager(null, new UDPConnector("127.0.0.1", 3333, false));
The first attribute is null because this is where normally the incoming connector would go but we won't need one now.
Next we have to create our OSCMessages and send them via the OSCManager.
-
var myMsg:OSCMessage = new OSCMessage();
-
myMsg.address = "/my/msg/target";
-
myMsg.addArgument("s", "Hello World");
-
-
oscManager.sendOSCPacket(myMsg);
This will send a message containing a string "Hello World" to the OSC address "/my/msg/target". Currently the following datatypes are supported:
- s A string
- i An integer
- f A float
- b A blob / ByteArray
- t An OSCTimetag
- d A double
- c An ascii character
- r A RGBA color
- T A boolean true. You don't have to specify a value for this type.
- F A boolean false. You don't have to specify a value for this type.
- N A null value. You don't have to specify a value for this type.
- I Infinity. You don't have to specify a value for this type.
You can also add onedimensional OSC arrays to an OSCMessage like this:
-
myMsg.addArguments("i[ssi]", [123, "Hello World", "Oh hai", 123]);
You can also wrap multiple OSCMessages into an OSCBundle like this:
-
var myBundle:OSCBundle = new OSCBundle();
-
myBundle.addPacket(myMsg);
-
myBundle.addPacket(myMsg2);
-
[...]
-
-
oscManager.sendOSCPacket(myBundle);
You should be able now to send osc instructions to any kind of OSC devices via UDP. If you happen to run into any problems write a comment or send me a mail.
September 24th, 2010 - 18:05
Hi, I’m looking to simply receive osc messages from another device and display the results in Flash. Do you have an example of doing this?
September 24th, 2010 - 19:25
Hi I don’t have an example at hand but it is quite simple. All you have to do is implement the org.tuio.osc.IOSCListener interface and add an instance of that class to the OSCManager via yourOSCManager.addMethod(“/some/osc/address”, new YourOSCListener()) which will give you all OSCMessages addressed to “/some/osc/address” or yourOSCManager.addMsgListener(new YourOSCListener()) which will give you all received OSCMessages.
If you have any additional questions just ask.
September 24th, 2010 - 20:39
Great, got it! It is pretty simple. Here’s what did it:
oscListener = new MyOSCListener()
oscListener.addEventListener(“MESSAGE”,handle_Manager_Message);
oscManager = new OSCManager(new UDPConnector(“127.0.0.1″,7780,true),null,false);
oscManager.addMsgListener(oscListener)
oscManager.start();
MyOSCListener looks like this:
package com{
import org.tuio.*;
import org.tuio.osc.*;
import org.tuio.connectors.*;
import flash.display.MovieClip;
import flash.events.Event;
public class MyOSCListener extends MovieClip implements IOSCListener {
var myValue:Number = 0;
public function acceptOSCMessage(oscmsg:OSCMessage):void{
myValue = oscmsg.arguments[0];
dispatchEvent(new Event(“MESSAGE”))
}
public function getValue():String{
return(myValue.toString())
}
}
}
September 25th, 2010 - 12:34
well done! and thanks for posting the code.
December 8th, 2010 - 15:17
Hi!
Jeff, how and where do you call the method ‘acceptOSCMessage’ of your class ‘MyOSCListener’ ?
December 8th, 2010 - 15:43
That is done by the OSCManager. The function ‘acceptOSCMessage’ ist the callback method defined in the IOSCListener interface and is called by the OSCManager if aninstance of your IOSCListener implementation is added to to it as listener via oscManager.addMsgListener(…)
December 8th, 2010 - 16:28
Thanks for your reply !
January 13th, 2011 - 07:23
I tried implementing what Jeff did to receive messages from OSCemote from an iPhone but had no luck. Is the “MESSAGE” suppose to be the message I want to look for from OSCemote, or perhaps Jeff your handle_Manager_Message function does something that I am not doing. I tried to just trace the results in Flash but nothing it being read.
January 13th, 2011 - 18:51
Make sure the OSCConnector is set to your IP or “0.0.0.0″ if you want to receive messages from the network. Also check if you use the right port.
February 18th, 2011 - 19:07
Hi,
I am trying to send some audio bytearray data. Is there a maximum size that I am allowed, as I get an error:
Socket message too long” errorID=3224]
Thanks for your help
February 18th, 2011 - 20:45
It seems like you packet is too big, but I can’t verify this for sure because adobes doc on this error is pretty much nonexistent and in the DatagramSocket doc it says, that if the message is too big it is just dropped and nothing should happen …
How big is your packet? You can check that via yourOSCBundle.getbytes().length. Make sure that it is smaller than 512bytes
July 27th, 2011 - 21:23
I suppose sending OSC only works with the UDP connection? Since udp-tcp-bridge and udp-flashlc-bridge are one direction only?
@Jeff: Thanks for the example, was a helpful start in receiving some OSC data.
July 28th, 2011 - 19:41
Yes sending OSC currently only works with the UDPConnector although you can send OSC between two flash/air programs with the LCConnector. Have a look at the instantiation parameters of the LCConnector, you can suply an ingoing and an outgoing name there. So in the sending movie you simply call new LCConnector() and in the receiving movie you call new LCConnector(“_OscDataStreamOut”)
August 16th, 2011 - 15:23
Hi gimmix,
thx a lot for the very helpful library! I wrote an example how to connect MaxMsp and Flash via OSC ( http://blog.derhess.de/2011/08/16/flash-talks-to-max-msp-via-osc/ ). I used successfully your library, but I had a problem with your UDPConnector class. During development I had a “binding port” problem in Air. This error ocurred because the UDPConnector never closed the port. I wanted to do this manually. But unfortunately the UDPConnector class does not provide a close() function. For this reason I wanted to write my own Connector class based on your UDPConnector class. To inherit from your class was not possible because all member variables are declared as private. Therefore I wrote a close() function directly into the UDP Connector class. This was not so clean… Maybe it would be cool to add a standard close function to the UDPConnector class in a new update of your library?! What do you think?
Best regards
Flo
August 16th, 2011 - 19:14
Hi, nice article and cool project. I am glad to see projects using my library

You are totally right the missing close() is quite annoying especially since it only happens when runnining the application outside of the development environment. I actually fixed that during one of my projects using the library but totally forgot to commit it to the code base so thanks for the reminder
I will probably extend the IOSConnector interface since all current connectors should be closed properly on exiting the application.
Thanks again and keep up the good work.
September 23rd, 2011 - 05:29
Hi All – Is it possible to send and receive messages on the same ip/port ?
I tried doing something like this:
var manager = new OSCManager(new UDPConnector(“192.168.1.255″, 3333, true), new UDPConnector(“192.168.1.255″, 3333, false));
But this just causes errors.
Any help would be greatly appreciated.
September 23rd, 2011 - 21:49
Hi, what kind of error do you get?
Is “192.168.1.255″ the IP of your computer? If yes this should / could work. If it is the IP of another computer in you LAN this will not work since you can only bind to ports on the system the app is running on.
September 24th, 2011 - 12:13
Hi gimmix – I was attempting to use 1.255 to send a broadcast message that other apps on the network would be listening for (including the one sending it) to synchronize an event. But I guess this is not possible?
September 24th, 2011 - 13:08
afaik 255.255.255.255 is the regular broadcast IP and it seems to work fine for me.
here’s a little snippet:
var inConn:UDPConnector = new UDPConnector(“your IP or 0.0.0.0″, 3333);
September 25th, 2011 - 02:15
Thanks for that! I will test it out in my project and let you know how it goes. Much appreciated.
September 26th, 2011 - 08:24
Hi gimmix – I was able to get an AIR app on 2 different machines to send and receive no problem – on PC. I tried the same thing on Mac but when trying to send a packet (.sendOSCPacket) it throws an error:
Error: Error #2031: Socket Error.
at flash.net::DatagramSocket/internalSend()
at flash.net::DatagramSocket/send()
…..etc.
If I have one app on a PC and one on a Mac, the Mac one does still receive the packets – it’s just sending on Mac that is failing.
Any thoughts?
September 26th, 2011 - 19:21
Well it looks like sending to a broadcast address isn’t officially supported by the DatagramSocket so you are lucky it works for windows.
What you could try is using a TUIO multiplexer like Throng
September 27th, 2011 - 02:33
thanks gimmix – appreciate your time.
October 17th, 2011 - 17:58
I’ve got a kinda stupid problem and I don’t know how to fix it:
When I copy-paste the above code I get the following error : 1180: Call to a possibly undefined method UDPConnector.
I’ve put the org directory in the same root as the Fla file.
Any idea what goes wrong?
November 21st, 2011 - 04:40
Chronoless, do you have the line “import org.tuio.connectors.UDPConnector;”?
April 6th, 2012 - 02:40
Hum… I’m getting
Error: Error #2002: Operation attempted on invalid socket.
at flash.net::DatagramSocket/internalBind()
at flash.net::DatagramSocket/bind()
at org.tuio.connectors.udp::OSCDatagramSocket()[C:\Users\phoenix\Documents\gameDev\OSC\tuio_as3_v_0_8\org\tuio\connectors\udp\OSCDatagramSocket.as:24]
at org.tuio.connectors::UDPConnector()[C:\Users\phoenix\Documents\gameDev\OSC\tuio_as3_v_0_8\org\tuio\connectors\UDPConnector.as:52]
at OSCExample()[C:\Users\phoenix\Documents\gameDev\starlingDev\OSCExample\src\OSCExample.as:37]
at runtime::ContentPlayer/loadInitialContent()
at runtime::ContentPlayer/playRawContent()
at runtime::ContentPlayer/playContent()
at runtime::AppRunner/run()
at ADLAppEntry/run()
at global/runtime::ADLEntry()
[SWF] OSCExample.swf – 99,439 bytes after decompression
April 6th, 2012 - 18:26
This can happen if you didn’t close the connector properly after a previous execution of the app. -> See: UDPConnector.close()
This will also happen if the IP or Port is invalid or already in use.
To temporarily fix this you can open the Taskmanager and kill all flash or air tasks that are still running in the background if you encounter this error.