@speechly/browser-client
Advanced tools
Comparing version 1.0.8 to 1.0.9
@@ -20,10 +20,2 @@ | ||
/** | ||
* The client establishes a WebSocket connection to SLU API. | ||
* | ||
* @param token - login token in JWT format, which was e.g. cached from previous session. | ||
* If the token is not provided or is invalid, a new token will be fetched instead. | ||
* @param targetSampleRate - sample rate of audio to be sent. | ||
*/ | ||
connect(token: string, targetSampleRate: number): void; | ||
/** | ||
* Initialises the client. | ||
@@ -113,2 +105,6 @@ * | ||
/** | ||
* Esteblish websocket connection | ||
*/ | ||
private connect; | ||
/** | ||
* Initializes the client, by initializing the microphone and establishing connection to the API. | ||
@@ -131,3 +127,3 @@ * | ||
*/ | ||
startContext(appId: string): Promise<string>; | ||
startContext(appId?: string): Promise<string>; | ||
private _startContext; | ||
@@ -134,0 +130,0 @@ /** |
{ | ||
"name": "@speechly/browser-client", | ||
"version": "1.0.8", | ||
"version": "1.0.9", | ||
"description": "Browser client for Speechly API", | ||
@@ -5,0 +5,0 @@ "private": false, |
@@ -45,2 +45,6 @@ import { ClientOptions, StateChangeCallback, SegmentChangeCallback, TentativeTranscriptCallback, TranscriptCallback, TentativeEntitiesCallback, EntityCallback, IntentCallback } from './types'; | ||
/** | ||
* Esteblish websocket connection | ||
*/ | ||
private connect; | ||
/** | ||
* Initializes the client, by initializing the microphone and establishing connection to the API. | ||
@@ -63,3 +67,3 @@ * | ||
*/ | ||
startContext(appId: string): Promise<string>; | ||
startContext(appId?: string): Promise<string>; | ||
private _startContext; | ||
@@ -66,0 +70,0 @@ /** |
@@ -152,3 +152,3 @@ "use strict"; | ||
const apiUrl = generateWsUrl((_g = options.apiUrl) !== null && _g !== void 0 ? _g : defaultApiUrl, language, (_h = options.sampleRate) !== null && _h !== void 0 ? _h : microphone_1.DefaultSampleRate); | ||
this.apiClient = (_j = options.apiClient) !== null && _j !== void 0 ? _j : new websocket_1.WebWorkerController(apiUrl); | ||
this.apiClient = (_j = options.apiClient) !== null && _j !== void 0 ? _j : new websocket_1.WebWorkerController(); | ||
this.storage = (_k = options.storage) !== null && _k !== void 0 ? _k : new storage_1.LocalStorage(); | ||
@@ -163,4 +163,3 @@ this.deviceId = this.storage.getOrSet(deviceIdStorageKey, uuid_1.v4); | ||
this.storage.set(authTokenKey, this.authToken); | ||
// Esteblish websocket connection | ||
this.apiClient.connect(this.authToken, this.sampleRate); | ||
this.connect(apiUrl); | ||
}).catch(err => { throw err; }); | ||
@@ -170,3 +169,3 @@ } | ||
this.authToken = storedToken; | ||
this.apiClient.connect(this.authToken, this.sampleRate); | ||
this.connect(apiUrl); | ||
} | ||
@@ -188,2 +187,13 @@ if (window.AudioContext !== undefined) { | ||
/** | ||
* Esteblish websocket connection | ||
*/ | ||
connect(apiUrl) { | ||
this.apiClient.postMessage({ | ||
type: 'INIT', | ||
apiUrl: apiUrl, | ||
authToken: this.authToken, | ||
targetSampleRate: this.sampleRate, | ||
}); | ||
} | ||
/** | ||
* Initializes the client, by initializing the microphone and establishing connection to the API. | ||
@@ -190,0 +200,0 @@ * |
@@ -152,10 +152,2 @@ /** | ||
/** | ||
* The client establishes a WebSocket connection to SLU API. | ||
* | ||
* @param token - login token in JWT format, which was e.g. cached from previous session. | ||
* If the token is not provided or is invalid, a new token will be fetched instead. | ||
* @param targetSampleRate - sample rate of audio to be sent. | ||
*/ | ||
connect(token: string, targetSampleRate: number): void; | ||
/** | ||
* Initialises the client. | ||
@@ -162,0 +154,0 @@ * |
import { APIClient, ResponseCallback, CloseCallback } from './types'; | ||
export declare class WebWorkerController implements APIClient { | ||
private readonly apiUrl; | ||
private authToken?; | ||
private worker?; | ||
private readonly worker; | ||
private resolveInitialization?; | ||
@@ -13,4 +11,3 @@ private startCbs; | ||
onClose(cb: CloseCallback): void; | ||
constructor(apiUrl: string); | ||
connect(token: string, targetSampleRate: number): void; | ||
constructor(); | ||
initialize(sourceSampleRate: number): Promise<void>; | ||
@@ -17,0 +14,0 @@ close(): Promise<void>; |
@@ -18,3 +18,3 @@ "use strict"; | ||
class WebWorkerController { | ||
constructor(apiUrl) { | ||
constructor() { | ||
this.startCbs = []; | ||
@@ -60,3 +60,6 @@ this.stopCbs = []; | ||
}; | ||
this.apiUrl = apiUrl; | ||
const blob = new Blob([worker_1.default], { type: 'text/javascript' }); | ||
const blobURL = window.URL.createObjectURL(blob); | ||
this.worker = new Worker(blobURL); | ||
this.worker.addEventListener('message', this.onWebsocketMessage); | ||
} | ||
@@ -69,24 +72,5 @@ onResponse(cb) { | ||
} | ||
connect(token, targetSampleRate) { | ||
this.authToken = token; | ||
if (this.worker !== undefined) { | ||
throw Error('Cannot initialize an already initialized worker'); | ||
} | ||
const blob = new Blob([worker_1.default], { type: 'text/javascript' }); | ||
const blobURL = window.URL.createObjectURL(blob); | ||
this.worker = new Worker(blobURL); | ||
this.worker.postMessage({ | ||
type: 'INIT', | ||
apiUrl: this.apiUrl, | ||
authToken: this.authToken, | ||
targetSampleRate, | ||
}); | ||
if (this.worker != null) { | ||
this.worker.addEventListener('message', this.onWebsocketMessage); | ||
} | ||
} | ||
initialize(sourceSampleRate) { | ||
var _a; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
(_a = this.worker) === null || _a === void 0 ? void 0 : _a.postMessage({ | ||
this.worker.postMessage({ | ||
type: 'SET_SOURSE_SAMPLE_RATE', | ||
@@ -103,10 +87,8 @@ sourceSampleRate, | ||
return new Promise((resolve, reject) => { | ||
if (this.worker != null) { | ||
this.worker.postMessage({ | ||
type: 'CLOSE', | ||
code: 1000, | ||
message: 'Client has ended the session', | ||
}); | ||
resolve(); | ||
} | ||
this.worker.postMessage({ | ||
type: 'CLOSE', | ||
code: 1000, | ||
message: 'Client has ended the session', | ||
}); | ||
resolve(); | ||
}); | ||
@@ -118,3 +100,2 @@ }); | ||
return new Promise((resolve, reject) => { | ||
var _a, _b; | ||
this.startCbs.push((err, id) => { | ||
@@ -129,6 +110,6 @@ if (err !== undefined) { | ||
if (appId != null) { | ||
(_a = this.worker) === null || _a === void 0 ? void 0 : _a.postMessage({ type: 'START_CONTEXT', appId }); | ||
this.worker.postMessage({ type: 'START_CONTEXT', appId }); | ||
} | ||
else { | ||
(_b = this.worker) === null || _b === void 0 ? void 0 : _b.postMessage({ type: 'START_CONTEXT' }); | ||
this.worker.postMessage({ type: 'START_CONTEXT' }); | ||
} | ||
@@ -141,3 +122,2 @@ }); | ||
return new Promise((resolve, reject) => { | ||
var _a; | ||
this.stopCbs.push((err, id) => { | ||
@@ -151,3 +131,3 @@ if (err !== undefined) { | ||
}); | ||
(_a = this.worker) === null || _a === void 0 ? void 0 : _a.postMessage({ type: 'STOP_CONTEXT' }); | ||
this.worker.postMessage({ type: 'STOP_CONTEXT' }); | ||
}); | ||
@@ -157,8 +137,6 @@ }); | ||
postMessage(message) { | ||
var _a; | ||
(_a = this.worker) === null || _a === void 0 ? void 0 : _a.postMessage(message); | ||
this.worker.postMessage(message); | ||
} | ||
sendAudio(audioChunk) { | ||
var _a; | ||
(_a = this.worker) === null || _a === void 0 ? void 0 : _a.postMessage({ type: 'AUDIO', payload: audioChunk }); | ||
this.worker.postMessage({ type: 'AUDIO', payload: audioChunk }); | ||
} | ||
@@ -165,0 +143,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
164757
2994