Home Reference Source Test
import OSC from 'osc-js/src/osc.js'
public class | source

OSC

Test:

Constructor Summary

Public Constructor
public

constructor(options: object)

Create an OSC instance with given options

Member Summary

Private Members
private
private

Method Summary

Public Methods
public

close(): *

Close connection.

public

off(eventName: string, subscriptionId: number): boolean

Unsubscribe an event listener

public

on(eventName: string, callback: function): number

Listen to a status-change event or incoming OSC message with address pattern matching

public

open(options: object): *

Open network socket with plugin.

public

send(packet: Packet | Bundle | Message, options: object): *

Send an OSC Packet, Bundle or Message.

public

Returns the current status of the connection.

Public Constructors

public constructor(options: object) source

Create an OSC instance with given options

Params:

NameTypeAttributeDescription
options object
  • optional

Custom options

options.discardLateMessages boolean
  • optional
  • default: false

Ignore incoming messages when given timetag lies in the past

options.plugin Plugin
  • optional
  • default: WebsocketClientPlugin

Add a connection plugin to this interface, defaults to a plugin with Websocket client. Open README.md for further information on how to handle plugins or how to write your own with the Plugin API

Example:

const osc = new OSC() // default options with Websocket client
const osc = new OSC({ discardLateMessages: true })
const websocketPlugin = new OSC.WebsocketClientPlugin()
const osc = new OSC({ plugin: websocketPlugin })

Private Members

private eventHandler: EventHandler source

private options: object source

Public Methods

public close(): * source

Close connection. This method is used by plugins and is not available without (see Plugin API for more information)

Return:

*

Test:

public off(eventName: string, subscriptionId: number): boolean source

Unsubscribe an event listener

Params:

NameTypeAttributeDescription
eventName string

Event name or OSC address pattern

subscriptionId number

The subscription id

Return:

boolean

Success state

Example:

const listenerId = osc.on('error', message => {
  console.log(message)
})
osc.off('error', listenerId) // unsubscribe from error event

Test:

public on(eventName: string, callback: function): number source

Listen to a status-change event or incoming OSC message with address pattern matching

Params:

NameTypeAttributeDescription
eventName string

Event name or OSC address pattern

callback function

Function which is called on notification

Return:

number

Subscription id (needed to unsubscribe)

Example:

// will be called when server receives /in!trument/* for example
osc.on('/instrument/1', message => {
  console.log(message)
})
// will be called for every message since it uses the wildcard symbol
osc.on('*', message => {
  console.log(message)
})
// will be called on network socket error
osc.on('error', message => {
  console.log(message)
})

Test:

public open(options: object): * source

Open network socket with plugin. This method is used by plugins and is not available without (see Plugin API for more information)

Params:

NameTypeAttributeDescription
options object
  • optional

Custom global options for plugin instance

Return:

*

Example:

const osc = new OSC({ plugin: new OSC.DatagramPlugin() })
osc.open({ host: '127.0.0.1', port: 8080 })

Test:

public send(packet: Packet | Bundle | Message, options: object): * source

Send an OSC Packet, Bundle or Message. This method is used by plugins and is not available without (see Plugin API for more information)

Params:

NameTypeAttributeDescription
packet Packet | Bundle | Message

OSC Packet, Bundle or Message instance

options object
  • optional

Custom options

Return:

*

Example:

const osc = new OSC({ plugin: new OSC.DatagramPlugin() })
osc.open({ host: '127.0.0.1', port: 8080 })

const message = new OSC.Message('/test/path', 55.1, 57)
osc.send(message)

// send message again to custom address
osc.send(message, { host: '192.168.178.115', port: 9001 })

Test:

public status(): number source

Returns the current status of the connection. See STATUS for different possible states. This method is used by plugins and is not available without (see Plugin API for more information)

Return:

number

Status identifier

Example:

import OSC, { STATUS } from 'osc'
const osc = new OSC()
if (osc.status() === STATUS.IS_CONNECTING) {
  // do something
}

Test: