Listens to a single port for incoming messages through a specific protocol and parses it into a plain javascript object. Then passes this object along to the dispatcher. If a response comes back, the dock serializes it to match the message format and sends it off to the original device.
Docks must extend the base Dock class.
process()
.validate(message) - Validates the message length against the configured max and performs a format check. This method can be overriden if needed. Returns true
if valid, false
otherwise.
parse(data, meta) - Parses the message and converts it to a javascript object. This is a default implementation provided for convenience that can and should be overriden to suit your needs. Returns the parsed data object or false
if the message couldn't be parsed.
tag1|subtag1|02,56,58,8|subtag2|sds,sd,wtr,ghd
This format is the standard for passing data through Iris components.
{
tag: // A string, the message tag
meta: // An object with additional data, such as the IP address that the message came from
// Must be set by the child class
data: // An object with the actual parsed data
}
meta
property meta: {
ip: '127.0.0.1'
}
data
property data: {
subtag1: ['02', '56', '58', '8'],
subtag2: ['sds', 'sd', 'wtr', 'ghd']
}
process(message, meta, callback) - Calls validate()
and parse()
and sends the result to the dispatcher. Should be called by listen()
. The callback is optional and will be called with the response if the handler produces one.
encode(response) - Serializes the response. This is a default implementation provided for convenience that can and should be overriden to suit your needs. Returns the encoded message or false
if the encoding was unsuccessful.
How it works:
|subtag1|value1,value2|subtag2|value3,value4,value5
.The separators can be configured, just like with the parser.
reply(response) - If send()
if defined, calls encode()
and pipes the result to send()
.
path (string) - Returns the path of the dock file.
get path() {
return __filename;
}
const dock = require('iris-dock-myAwesomeDock');
dock.on('data', function(event){
console.log('data', event.data);
});
Every event
object contains the following properties:
validate()
filters, or when the message either is not a string, or does not have a toString()
method.{
port: 5000,
parser: {
subtagSeparator: '#', // example, default is |
dataSeparator: '.' // example, default is ,
},
encoder: {
subtagSeparator: '#', // idam parser
dataSeparator: '.' // idem parser
},
maxMessageLength: 100
}
|
for subtagSeparator
and ,
for dataSeparator
.300
.