microsoft-cognitiveservices-speech-sdk
Version:
Microsoft Cognitive Services Speech SDK for JavaScript
1 lines • 10.6 kB
Source Map (JSON)
{"version":3,"sources":["src/sdk/Connection.ts"],"names":[],"mappings":"AAqBA,OAAO,EACH,mBAAmB,EACnB,0BAA0B,EAC1B,uBAAuB,EACvB,UAAU,EACV,gBAAgB,EAChB,iBAAiB,EACpB,MAAM,cAAc,CAAC;AAEtB;;;;;;;;;;;;GAYG;AACH,qBAAa,UAAU;IACnB,OAAO,CAAC,gBAAgB,CAA+C;IACvE,OAAO,CAAC,iBAAiB,CAAc;IACvC,OAAO,CAAC,wBAAwB,CAAc;IAE9C;;;;OAIG;WACW,cAAc,CAAC,UAAU,EAAE,UAAU,GAAG,uBAAuB,GAAG,UAAU;IAS1F;;;;OAIG;WACW,eAAe,CAAC,WAAW,EAAE,iBAAiB,GAAG,UAAU;IAUzE;;;;;;;OAOG;IACI,cAAc,CAAC,EAAE,CAAC,EAAE,MAAM,IAAI,EAAE,GAAG,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,GAAG,IAAI;IAI3E;;;;;OAKG;IACI,eAAe,CAAC,EAAE,CAAC,EAAE,MAAM,IAAI,EAAE,GAAG,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,GAAG,IAAI;IAQ5E;;;;;;OAMG;IACI,kBAAkB,CAAC,IAAI,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI;IAkBnG;;;;;;;OAOG;IACI,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,WAAW,EAAE,OAAO,CAAC,EAAE,MAAM,IAAI,EAAE,KAAK,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,GAAG,IAAI;IAIjI;;;;OAIG;IACI,sBAAsB,EAAE,CAAC,IAAI,EAAE,gBAAgB,KAAK,IAAI,CAAC;IAEhE;;OAEG;IACI,eAAe,EAAE,CAAC,IAAI,EAAE,0BAA0B,KAAK,IAAI,CAAC;IAEnE;;OAEG;IACI,WAAW,EAAE,CAAC,IAAI,EAAE,0BAA0B,KAAK,IAAI,CAAC;IAE/D;;OAEG;IACI,SAAS,EAAE,CAAC,IAAI,EAAE,mBAAmB,KAAK,IAAI,CAAC;IAEtD;;OAEG;IACI,YAAY,EAAE,CAAC,IAAI,EAAE,mBAAmB,KAAK,IAAI,CAAC;IAEzD;;OAEG;IACI,KAAK,IAAI,IAAI;IAIpB,OAAO,CAAC,WAAW;CA2BtB","file":"Connection.d.ts","sourcesContent":["//\r\n// Copyright (c) Microsoft. All rights reserved.\r\n// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.\r\n//\r\n\r\nimport {\r\n ServiceRecognizerBase,\r\n SynthesisAdapterBase,\r\n} from \"../common.speech/Exports.js\";\r\nimport {\r\n ConnectionEvent,\r\n ConnectionMessageReceivedEvent,\r\n ConnectionMessageSentEvent,\r\n IDetachable,\r\n marshalPromiseToCallbacks,\r\n ServiceEvent,\r\n} from \"../common/Exports.js\";\r\nimport {\r\n ConnectionMessageImpl\r\n} from \"./ConnectionMessage.js\";\r\nimport { Contracts } from \"./Contracts.js\";\r\nimport {\r\n ConnectionEventArgs,\r\n ConnectionMessageEventArgs,\r\n ConversationTranscriber,\r\n Recognizer,\r\n ServiceEventArgs,\r\n SpeechSynthesizer,\r\n} from \"./Exports.js\";\r\n\r\n/**\r\n * Connection is a proxy class for managing connection to the speech service of the specified Recognizer.\r\n * By default, a Recognizer autonomously manages connection to service when needed.\r\n * The Connection class provides additional methods for users to explicitly open or close a connection and\r\n * to subscribe to connection status changes.\r\n * The use of Connection is optional, and mainly for scenarios where fine tuning of application\r\n * behavior based on connection status is needed. Users can optionally call Open() to manually set up a connection\r\n * in advance before starting recognition on the Recognizer associated with this Connection.\r\n * If the Recognizer needs to connect or disconnect to service, it will\r\n * setup or shutdown the connection independently. In this case the Connection will be notified by change of connection\r\n * status via Connected/Disconnected events.\r\n * Added in version 1.2.1.\r\n */\r\nexport class Connection {\r\n private privInternalData: ServiceRecognizerBase | SynthesisAdapterBase;\r\n private privEventListener: IDetachable;\r\n private privServiceEventListener: IDetachable;\r\n\r\n /**\r\n * Gets the Connection instance from the specified recognizer.\r\n * @param recognizer The recognizer associated with the connection.\r\n * @return The Connection instance of the recognizer.\r\n */\r\n public static fromRecognizer(recognizer: Recognizer | ConversationTranscriber): Connection {\r\n const recoBase = recognizer.internalData as ServiceRecognizerBase;\r\n const ret: Connection = new Connection();\r\n\r\n ret.privInternalData = recoBase;\r\n ret.setupEvents();\r\n return ret;\r\n }\r\n\r\n /**\r\n * Gets the Connection instance from the specified synthesizer.\r\n * @param synthesizer The synthesizer associated with the connection.\r\n * @return The Connection instance of the synthesizer.\r\n */\r\n public static fromSynthesizer(synthesizer: SpeechSynthesizer): Connection {\r\n const synthBase: SynthesisAdapterBase = synthesizer.internalData as SynthesisAdapterBase;\r\n\r\n const ret: Connection = new Connection();\r\n\r\n ret.privInternalData = synthBase;\r\n ret.setupEvents();\r\n return ret;\r\n }\r\n\r\n /**\r\n * Starts to set up connection to the service.\r\n * Users can optionally call openConnection() to manually set up a connection in advance before starting recognition on the\r\n * Recognizer associated with this Connection. After starting recognition, calling Open() will have no effect\r\n *\r\n * Note: On return, the connection might not be ready yet. Please subscribe to the Connected event to\r\n * be notified when the connection is established.\r\n */\r\n public openConnection(cb?: () => void, err?: (error: string) => void): void {\r\n marshalPromiseToCallbacks(this.privInternalData.connect(), cb, err);\r\n }\r\n\r\n /**\r\n * Closes the connection the service.\r\n * Users can optionally call closeConnection() to manually shutdown the connection of the associated Recognizer.\r\n *\r\n * If closeConnection() is called during recognition, recognition will fail and cancel with an error.\r\n */\r\n public closeConnection(cb?: () => void, err?: (error: string) => void): void {\r\n if (this.privInternalData instanceof SynthesisAdapterBase) {\r\n throw new Error(\"Disconnecting a synthesizer's connection is currently not supported\");\r\n } else {\r\n marshalPromiseToCallbacks(this.privInternalData.disconnect(), cb, err);\r\n }\r\n }\r\n\r\n /**\r\n * Appends a parameter in a message to service.\r\n * Added in version 1.12.1.\r\n * @param path The path of the network message.\r\n * @param propertyName Name of the property\r\n * @param propertyValue Value of the property. This is a json string.\r\n */\r\n public setMessageProperty(path: string, propertyName: string, propertyValue: string | object): void {\r\n Contracts.throwIfNullOrWhitespace(propertyName, \"propertyName\");\r\n\r\n if (this.privInternalData instanceof ServiceRecognizerBase) {\r\n if (path.toLowerCase() !== \"speech.context\") {\r\n throw new Error(\"Only speech.context message property sets are currently supported for recognizer\");\r\n } else {\r\n this.privInternalData.speechContext.setSection(propertyName, propertyValue);\r\n }\r\n } else if (this.privInternalData instanceof SynthesisAdapterBase) {\r\n if (path.toLowerCase() !== \"synthesis.context\") {\r\n throw new Error(\"Only synthesis.context message property sets are currently supported for synthesizer\");\r\n } else {\r\n this.privInternalData.synthesisContext.setSection(propertyName, propertyValue);\r\n }\r\n }\r\n }\r\n\r\n /**\r\n * Sends a message to the speech service.\r\n * Added in version 1.13.0.\r\n * @param path The WebSocket path of the message\r\n * @param payload The payload of the message. This is a json string or a ArrayBuffer.\r\n * @param success A callback to indicate success.\r\n * @param error A callback to indicate an error.\r\n */\r\n public sendMessageAsync(path: string, payload: string | ArrayBuffer, success?: () => void, error?: (error: string) => void): void {\r\n marshalPromiseToCallbacks(this.privInternalData.sendNetworkMessage(path, payload), success, error);\r\n }\r\n\r\n /**\r\n * Any message from service that is not being processed by any other top level recognizers.\r\n *\r\n * Will be removed in 2.0.\r\n */\r\n public receivedServiceMessage: (args: ServiceEventArgs) => void;\r\n\r\n /**\r\n * Any message received from the Speech Service.\r\n */\r\n public messageReceived: (args: ConnectionMessageEventArgs) => void;\r\n\r\n /**\r\n * Any message sent to the Speech Service.\r\n */\r\n public messageSent: (args: ConnectionMessageEventArgs) => void;\r\n\r\n /**\r\n * The Connected event to indicate that the recognizer is connected to service.\r\n */\r\n public connected: (args: ConnectionEventArgs) => void;\r\n\r\n /**\r\n * The Disconnected event to indicate that the recognizer is disconnected from service.\r\n */\r\n public disconnected: (args: ConnectionEventArgs) => void;\r\n\r\n /**\r\n * Dispose of associated resources.\r\n */\r\n public close(): void {\r\n /* eslint-disable no-empty */\r\n }\r\n\r\n private setupEvents(): void {\r\n this.privEventListener = this.privInternalData.connectionEvents.attach((connectionEvent: ConnectionEvent): void => {\r\n if (connectionEvent.name === \"ConnectionEstablishedEvent\") {\r\n if (!!this.connected) {\r\n this.connected(new ConnectionEventArgs(connectionEvent.connectionId));\r\n }\r\n } else if (connectionEvent.name === \"ConnectionClosedEvent\") {\r\n if (!!this.disconnected) {\r\n this.disconnected(new ConnectionEventArgs(connectionEvent.connectionId));\r\n }\r\n } else if (connectionEvent.name === \"ConnectionMessageSentEvent\") {\r\n if (!!this.messageSent) {\r\n this.messageSent(new ConnectionMessageEventArgs(new ConnectionMessageImpl((connectionEvent as ConnectionMessageSentEvent).message)));\r\n }\r\n } else if (connectionEvent.name === \"ConnectionMessageReceivedEvent\") {\r\n if (!!this.messageReceived) {\r\n this.messageReceived(new ConnectionMessageEventArgs(new ConnectionMessageImpl((connectionEvent as ConnectionMessageReceivedEvent).message)));\r\n }\r\n }\r\n });\r\n\r\n this.privServiceEventListener = this.privInternalData.serviceEvents.attach((e: ServiceEvent): void => {\r\n if (!!this.receivedServiceMessage) {\r\n this.receivedServiceMessage(new ServiceEventArgs(e.jsonString, e.name));\r\n }\r\n });\r\n }\r\n}\r\n"]}