问题
I have a Processing application that must talk to a desktop Adobe AIR application. Can anyone suggest the best options considering the following?
If sockets are the answer, which type of Socket, UDP(Datagram Class), ServerSocket or just plain Socket?
-both apps reside on same machine
-latency important, smaller better
-signals being sent are small, consisting of 3 values
-communication is 1 way only, processing TO Adobe AIR
回答1:
On the Processing side you can use oscP5:
import oscP5.*;
import netP5.*;
OscP5 osc;
NetAddress where;
void setup() {
frameRate(25);text("click to send\nOSC",5,50);
osc = new OscP5(this,12000);
where = new NetAddress("127.0.0.1",8082);
}
void draw() {}
void mousePressed() {
OscMessage what = new OscMessage("/straps");
what.add(193.4509887695313);
osc.send(what, where);
}
In AIR you would use a DatagramSocket indeed. Luckily you can use something that with OSC packets like TUIO AS3's UDPConnector. Here's a basic as3 sample:
package
{
import flash.display.Sprite;
import flash.utils.getDefinitionByName;
import org.tuio.connectors.UDPConnector;
import org.tuio.osc.*;
public class BasicOSC extends Sprite implements IOSCConnectorListener
{
private var oscSocket:UDPConnector;
private const OSCSERVER:String = "127.0.0.1";
private const PORT:int = 8082;
public function BasicOSC()
{
try{
oscSocket = new UDPConnector(OSCSERVER,PORT);
oscSocket.addListener(this);
trace(this,"OSC ready");
}catch(e:Error){ trace(e.getStackTrace()); }
}
public function acceptOSCPacket(oscPacket:OSCPacket):void{
//handle OSC here
var message:OSCMessage = oscPacket as OSCMessage;
trace("message from :",message.address,"at",new Date());
for(var i:int = 0; i < message.arguments.length; i++)
trace("\targs["+i+"]",message.arguments[i]);
}
}
}
This is roughly what I used for some of these projects:
来源:https://stackoverflow.com/questions/23202572/adobe-air-desktop-application-communicate-with-processing-application