Skip to content

Node.js Server

Andreas Dzialocha edited this page Nov 1, 2018 · 11 revisions

WebSocket server example

This opens a WebSocket Server reachable on address ws://localhost:9912. You can parallely write an browser application now to send OSC data back and forth between clients and server.

const OSC = require('osc-js')

const plugin = new OSC.WebsocketServerPlugin({ port: 9912 })
const osc = new OSC({ plugin: plugin })

// listen for invoing messages

osc.on('/test', message => {
  console.log(message.args)
})

// sent messages frequently when socket is ready

osc.on('open', () => {
  setInterval(() => {
     osc.send(new OSC.Message('/response', Math.random()))
  }, 1000)
})

osc.open() // start server

UDP server example

const OSC = require('osc-js')

const options = { send: { port: 11245 } }
const osc = new OSC({ plugin: new OSC.DatagramPlugin(options) })

osc.on('open', () => {
  // send only this message to `localhost:9002`
  osc.send(new OSC.Message('/hello'), { port: 9002 })

  setInterval(() => {
     // send these messages to `localhost:11245`
     osc.send(new OSC.Message('/response', Math.random()))
  }, 1000)
})

osc.open({ port: 9912 }) // bind socket to localhost:9912