This method is used to create composite message types from other types, such as JSON, XML, etc.
// Register the json-xml-json-binary composite message type with the type helper
amps.TypeHelper.helper(
'compositejxjb',
amps.TypeHelper.compositeHelper('json', 'xml', 'json', 'binary')
);
A created ITypeHelper-compatible object.
This method provides a helper for parsing messages of different types. If a helper was provided by a user, registers it for future use.
var xmlTypeHelper = {
serialize: function(data) {
return [new XMLSerializer().serializeToString(data)];
},
deserialize: function(data) {
if (data.constructor === String) {
return new DOMParser().parseFromString(data);
}
else {
// Binary buffer, need to decode utf8
return new DOMParser().parseFromString(
decodeURIComponent(escape(Uint8ToString(data)))
);
}
}
};
// Register the above XML custom helper for parsing all XML messages automatically
amps.TypeHelper.helper('xml', xmlTypeHelper);
// create a JSON type helper that does not parse JSON data into native JS objects keeping it as a string
var jsonHelper = {
serialize: function(data) {
return [data];
},
deserialize: function(data) {
return JSON.stringify(data);
}
};
// override the default type helper
amps.TypeHelper.helper('json', jsonHelper);
amps.TypeHelper.helper(
'compositejjjbl',
amps.TypeHelper.compositeHelper('json', 'json', 'json', 'binary')
);
amps.TypeHelper.helper('nvfix').delimiter('%01');
var nvfixTypeHelper = {
serialize: function(data) {
// already formatted fix/nvfix string
if (typeof data === 'string') {
return [data];
}
// otherwise, we assume it's an object with keys and values
return [
Object.keys(data).map(function(key) {
return key + '=' + data[key];
}).join('\x01') + '\x01'
];
},
deserialize: function(data) {
var parsedData = {};
String.fromCharCode.apply(null, new Int8Array(data))
.split('\x01')
.slice(0, -1)
.map(function(keyValue) {
var keyValueTuple = keyValue.split('=');
var key = keyValueTuple[0];
// no '=' inside of the value
if (keyValueTuple.length === 2) {
parsedData[key] = keyValueTuple[1];
}
else {
parsedData[key] = keyValue.slice(key.length + 1);
}
});
return parsedData;
}
};
// Register the above NVFIX custom helper for parsing all NVFIX messages automatically
amps.TypeHelper.helper('nvfix', nvfixTypeHelper);
The type of message to handle.
A helper object with predefined functions that can be provided for custom parsing/handling of a message type.
The helper object assigned/registered.
This is a helper class that is used to register custom message types in order for the client to support them. By default, the Client supports the following message types:
Composite message types can be created using TypeHelper.compositeHelper method.
See TypeHelper.helper for examples on how to implement support for new types or custom type helpers for existing message types.