Skip to content

Plugin API

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

Check the current implementations of osc-js's default plugins: https:/adzialocha/osc-js/tree/master/src/plugin.

The notify method and EventHandler

Please read the source documentation on the notify method.

// example of notify method
const socket = dgram.createSocket('udp4')
socket.on('message', message => {
  this.notify(message)
})

Starter template

This is a starter template for an osc-js plugin:

/**
 * Status flags
 */
const STATUS = {
  IS_NOT_INITIALIZED: -1,
  IS_CONNECTING: 0,
  IS_OPEN: 1,
  IS_CLOSING: 2,
  IS_CLOSED: 3,
}
 
/**
 * Default options
 */
const defaultOptions = {}
 
/**
 * OSC plugin template
 */
export default class MyPlugin {
  /**
   * Create an OSC Plugin instance with given options.
   * @param {object} [options] Custom options
   */
  constructor(customOptions) {
    /** @type {object} options
     * @private
     */
    this.options = Object.assign({}, defaultOptions, customOptions)
 
    /**
     * @type {number} socketStatus
     * @private
     */
    this.socketStatus = STATUS.IS_NOT_INITIALIZED
 
    /**
     * @type {function} notify
     * @private
     */
    this.notify = () => {}
  }
 
  /**
   * Internal method to hook into osc library's
   * EventHandler notify method
   * @param {function} fn Notify callback
   * @private
   */
  registerNotify(fn) {
    this.notify = fn
  }
 
  /**
   * Returns the current status of the connection
   * @return {number} Status ID
   */
  status() {
    return this.socketStatus
  }
 
  /**
   * Bind to a hostname and port
   * @param {object} [customOptions] Custom options
   */
  open(customOptions) {
    const options = Object.assign({}, this.options, customOptions)
    // this.notify('open')
  }
 
  /**
   * Close socket
   */
  close() {
    // this.notify('close')
  }
 
  /**
   * Send an OSC Packet, Bundle or Message. Use options here for
   * custom receiver, otherwise the global options will be taken
   * @param {Uint8Array} binary Binary representation of OSC Packet
   * @param {object} [customOptions] Custom options
   */
  send(binary, customOptions) {
    const options = Object.assign({}, this.options, customOptions)
  }
}