Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Currently it's not possible to use UDP communication from javascript running in the browser. However, we can use websocket. 

...

websockets. 

First download the OSC browser library and added it script import to the document head in your index.html.

Code Block
languagexml
  <script language="javascript" type="text/javascript" src="../osc.min.js"></script>


In your p5j sketch, add the following code: 

Code Block
languagejs
const port = 8025;
const osc = new OSC();

setup() {
// setup OSC receiver
  osc.on('/text', msg => {
    handleMessage(msg);
  }
  );

  try {
    osc.open({
      port:
        port
    }
    );
  }
  catch (e) {
    console.log("Could not connect: " + e);
  }
}


function handleMessage(msg) {
 console.log((msg.args[0]); // print the 1st message item out to console
 console.log((msg.args[1]); // print the 2nd message item out to console
}