Skip to content

Instantly share code, notes, and snippets.

@automata
Created February 13, 2011 03:54
Show Gist options
  • Save automata/824420 to your computer and use it in GitHub Desktop.
Save automata/824420 to your computer and use it in GitHub Desktop.
testing OSC under UDP on FF4
// asking for permission to use XPCOM
try {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
} catch (e) {
alert("Error: " + e);
}
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
// output
function oscOutputClient(outputHost, outputPort) {
try {
this.transportService =
Components.classes["@mozilla.org/network/socket-transport-service;1"]
.getService(Components.interfaces.nsISocketTransportService);
this.transport =
this.transportService.createTransport(["udp"], 1, outputHost, outputPort, null);
var output = this.transport.openOutputStream(0, 4096, -1);
this.outputStream =
Components.classes["@mozilla.org/binaryoutputstream;1"]
.createInstance(Components.interfaces.nsIBinaryOutputStream);
this.outputStream.setOutputStream(output);
} catch (e) {
alert("Error starting client! Bailing.");
return null;
}
}
oscOutputClient.prototype = {
send: function(msg) {
this.outputStream.writeWStringZ(msg, msg.length);
},
close: function() {
this.outputStream.close();
}
}
// input
function oscInputClient(inputHost, inputPort) {
try {
var tm = Components.classes["@mozilla.org/thread-manager;1"]
.getService(Components.interfaces.nsIThreadManager);
this._uiThread = tm.mainThread;
this.transportService =
Components.classes["@mozilla.org/network/socket-transport-service;1"]
.getService(Components.interfaces.nsISocketTransportService);
this.transport =
this.transportService.createTransport(["udp"], 1, inputHost, inputPort, null);
this.transport.setTimeout(Components.interfaces.nsISocketTransport.TIMEOUT_CONNECT, 300);
this.transport.setTimeout(Components.interfaces.nsISocketTransport.TIMEOUT_READ_WRITE, 300);
this.transport.setEventSink(this, this._uiThread);
this.stream = this.transport.openInputStream(0, 0, 0);
this.inputStream =
Components.classes["@mozilla.org/binaryinputstream;1"]
.createInstance(Components.interfaces.nsIBinaryInputStream);
this.inputStream.setInputStream(this.stream);
this.asyncInput = this.stream.QueryInterface(Components.interfaces.nsIAsyncInputStream);
} catch (e) {
alert("Error: " + e);
return null;
}
}
oscInputClient.prototype = {
QueryInterface: XPCOMUtils.generateQI([Components.interfaces.nsITransportEventSink,
Components.interfaces.nsIOutputStreamCallback,
Components.interfaces.nsIInputStreamCallback]),
onStartRequest: function(request, context) {
},
onStopRequest: function(request, context, status) {
if (this.inputStream) {
try {
this.inputStream.close();
this.inputStream = null;
} catch (e) {
alert("Error: " + e);
}
}
},
onDataAvailable: function(request, context, inStream, offset, count) {
while (this.inputStream.available()) {
inputText += this.inputStream.read(count);
console.log(inputText);
}
},
onInputStreamReady : function(input) {
try {
var bytes = this.inputStream.readByteArray(input.available());
console.log("Received bytes: " + bytes);
this.asyncInput.asyncWait(this, 0, 0, this.uiThread);
} catch(e) {
console.log(e);
}
}
};
// sending OSC messages on localhost:4242
var foo = new oscOutputClient("127.0.0.1", 4242);
foo.send("/print,sfoo");
// receiving OSC messages from localhost:4343
bar = new oscInputClient('127.0.0.1', 4343);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment