src/lib/myScript/recognition/abstractWSRecognizer.ts
constructor()
|
getProtocol |
getProtocol()
|
Returns:
string
|
getSSL |
getSSL()
|
Returns:
boolean
|
setSSL |
setSSL(ssl: boolean)
|
Returns:
void
|
getHost |
getHost()
|
Get the recognition service host
Returns:
any
|
setHost |
setHost(host: string)
|
Set the recognition service host
Returns:
void
|
setUrl |
setUrl(url: string)
|
Returns:
void
|
getUrl |
getUrl()
|
Returns:
string
|
setCallback |
setCallback(callback: Function)
|
Returns:
void
|
getParameters |
getParameters()
|
Get parameters
Returns:
AbstractParameter
|
setParameters |
setParameters(parameters: AbstractParameter)
|
Set parameters
Parameters :
Returns:
void
|
getPrecision |
getPrecision()
|
Get precision
Returns:
number
|
setPrecision |
setPrecision(precision: number)
|
Set precision
Parameters :
Returns:
void
|
isClosed |
isClosed()
|
Returns:
boolean
|
isClosing |
isClosing()
|
Returns:
boolean
|
isOpen |
isOpen()
|
Returns:
boolean
|
isConnecting |
isConnecting()
|
Returns:
boolean
|
open |
open()
|
Open the socket
Returns:
void
|
close |
close()
|
Close the socket
Returns:
void
|
sendMessage |
sendMessage(message: any)
|
Send a message
Parameters :
Returns:
void
|
initWSRecognition |
initWSRecognition(applicationKey: string)
|
Initialize the WebSocket
Parameters :
Returns:
void
|
startWSRecognition |
startWSRecognition(components: any, parameters: any)
|
Start the WebSocket session
Parameters :
Returns:
void
|
continueWSRecognition |
continueWSRecognition(components: any, instanceId: string)
|
Continue the recognition
Parameters :
Returns:
void
|
takeUpHmacChallenge |
takeUpHmacChallenge(applicationKey: string, challenge: string, hmacKey: string)
|
Authenticate the WebSocket client end with a handshake of HMAC signature
Parameters :
Returns:
void
|
resetWSRecognition |
resetWSRecognition()
|
Reset the WebSocket recognition session
Returns:
void
|
Static _computeHmac |
_computeHmac(input: any, applicationKey: string, hmacKey: string)
|
Compute HMAC signature for server authentication
Parameters :
Returns:
void
|
Static _filterStrokes |
_filterStrokes(components: any, precision: any)
|
Returns:
void
|
_ssl |
_ssl: |
_wsInterface |
_wsInterface: |
parameters |
parameters: |
precision |
precision: |
import { NetworkWSInterface } from '../networking/networkWSInterface';
import { NetworkInterface } from '../networking/networkInterface';
import { AbstractParameter } from '../input/generic/abstractParameter';
import { InitRequestWSMessage } from '../input/generic/initRequestWSMessage';
import { ChallengeRequestWSMessage } from '../input/generic/challengeRequestWSMessage';
import { ResetRequestWSMessage } from '../input/generic/resetRequestWSMessage';
import { StrokeComponent } from '../input/generic/components/strokeComponent';
import * as CryptoJS from 'crypto-js';
export abstract class AbstractWSRecognizer {
_wsInterface: NetworkWSInterface;
_ssl: boolean;
parameters: AbstractParameter;
precision: number;
constructor() {
this._wsInterface = new NetworkWSInterface();
}
getProtocol(): string {
return this._ssl ? 'wss://' : 'ws://';
};
getSSL(): boolean {
return this._ssl;
};
setSSL(ssl: boolean) {
this._ssl = ssl;
this.setUrl(this.getProtocol() + this.getHost());
};
/**
* Get the recognition service host
*
* @method getHost
* @returns {string|String|*}
*/
getHost(): string | any {
return NetworkInterface.parseURL(this.getUrl()).host;
};
/**
* Set the recognition service host
*
* @method setHost
* @param {String}
*/
setHost(host?: string) {
if ((host !== undefined) && (host != this.getHost())) {
this.setUrl(this.getProtocol() + host);
}
};
setUrl(url: string) {
throw new Error('not implemented');
};
getUrl(): string {
return this._wsInterface.getUrl();
};
setCallback(callback: Function) { // jshint ignore:line
throw new Error('not implemented');
};
/**
* Get parameters
*
* @method getParameters
* @returns {AbstractParameter}
*/
getParameters(): AbstractParameter {
return this.parameters;
};
/**
* Set parameters
*
* @method setParameters
* @param {AbstractParameter} parameters
*/
setParameters(parameters: AbstractParameter) {
this.parameters = parameters;
};
/**
* Get precision
*
* @method getPrecision
* @returns {Number}
*/
getPrecision(): number {
return this.precision;
};
/**
* Set precision
*
* @method setPrecision
* @param {Number} precision
*/
setPrecision(precision: number) {
this.precision = precision;
};
isClosed(): boolean {
return this._wsInterface.isClosed();
};
isClosing(): boolean {
return this._wsInterface.isClosing();
};
isOpen(): boolean {
return this._wsInterface.isOpen();
};
isConnecting(): boolean {
return this._wsInterface.isConnecting();
};
/**
* Open the socket
*
* @method open
*/
open() {
this._wsInterface.open();
};
/**
* Close the socket
*
* @method close
*/
close() {
this._wsInterface.close();
};
/**
* Send a message
*
* @method sendMessage
* @param {AbstractWSMessage} message
*/
sendMessage(message) {
if (message.getComponents) {
AbstractWSRecognizer._filterStrokes(message.getComponents(), this.getPrecision());
} else if (message.getInputUnits) {
for (let i in message.getInputUnits()) {
AbstractWSRecognizer._filterStrokes(message.getInputUnits()[i].getComponents(), this.getPrecision());
}
}
this._wsInterface.send(message);
};
/**
* Initialize the WebSocket
*
* @method initWSRecognition
* @param {String} applicationKey
*/
initWSRecognition(applicationKey: string) {
let message = new InitRequestWSMessage();
message.setApplicationKey(applicationKey);
this.sendMessage(message);
};
/**
* Start the WebSocket session
*
* @abstract
* @param {any} [components]
* @param {any} [parameters]
*
* @memberof AbstractWSRecognizer
*/
abstract startWSRecognition(components, parameters?);
/**
* Continue the recognition
*
* @abstract
* @param {any} components
* @param {string} instanceId
*
* @memberof AbstractWSRecognizer
*/
abstract continueWSRecognition(components, instanceId: string)
/**
* Authenticate the WebSocket client end with a handshake of HMAC signature
*
* @method takeUpHmacChallenge
* @param {String} applicationKey
* @param {String} challenge
* @param {String} hmacKey
*/
takeUpHmacChallenge(applicationKey: string, challenge: string, hmacKey: string) {
let message = new ChallengeRequestWSMessage();
message.setApplicationKey(applicationKey);
message.setChallenge(challenge);
if (hmacKey) {
message.setHmacSignature(AbstractWSRecognizer._computeHmac(challenge, applicationKey, hmacKey));
}
this.sendMessage(message);
};
/**
* Reset the WebSocket recognition session
*
* @method resetWSRecognition
*/
resetWSRecognition() {
let message = new ResetRequestWSMessage();
this.sendMessage(message);
};
/**
* Compute HMAC signature for server authentication
*
* @private
* @method _computeHmac
* @param {String} input
* @param {String} applicationKey
* @param {String} hmacKey
*/
static _computeHmac(input, applicationKey: string, hmacKey: string) {
let jsonInput: string = (typeof input === 'object') ? JSON.stringify(input) : input;
return CryptoJS.HmacSHA512(jsonInput, applicationKey + hmacKey)
.toString();
};
static _filterStrokes(components, precision) {
components.forEach(function (currentValue) {
if (currentValue instanceof StrokeComponent) {
currentValue.toFixed(precision);
}
});
};
}