Device Monitoring Studio - Monitor, log and analyze data coming through PC ports and connections
Download Device Monitoring Studio Hide this button

Event Binding

Some of the objects available for scripting provide one or more events. For example, Serial Terminal Session Object exposes two events: ITerminalSession.sent and ITerminalSession.received. Remote Connection Manager provides the IRemoteHost.connected and IRemoteHost.disconnected events.

To bind a function to event, call the first function overload passing the function object that matches the required signature. This function returns an event id that user script may pass to the second event overload to unbind the event handler. It is not mandatory to unbind the handler before the target object is destroyed.

Once bound, the passed function object is executed each time the object event fires.

When the last statement of the global scope finishes execution, script execution is halted, but not stopped completely. This is done in order for script to be able to process scheduled asynchronous functions, bound events and promises continuations.

// Bind function to IRemoteHost.connected event
var bound_event_id = remote.connected(name => alert("Connected to " + name));

// Connect to remote computer
remote.connectServer("servername");

// Successful connection will cause our event handler to be called
// ...

// Optionally, disconnect our handler
remote.connected(bound_event_id);