diff --git a/package-lock.json b/package-lock.json index adb6c72..486a2c3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "ocast-sdk", - "version": "1.0.8", + "version": "1.1.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/src/channel/channel.ts b/src/channel/channel.ts index bf450f7..e99a3e5 100644 --- a/src/channel/channel.ts +++ b/src/channel/channel.ts @@ -14,7 +14,6 @@ * limitations under the License. */ -import {OCast} from "../ocast"; import {Transport} from "../protocol/transport"; import {TransportMessage} from "../protocol/transport.message"; import {EnumError} from "../type/enum.error"; @@ -32,6 +31,7 @@ const Log: Logger = Logger.getInstance(); */ export class Channel { protected static sequenceMessage: number = 1; + private waitingReplies: {[key: string]: (transport: Transport) => void} = {}; private ws: WebSocket = null; @@ -52,6 +52,14 @@ export class Channel { * @param {Transport} transport - message received */ public onMessage(transport: Transport) { + if (transport.type === EnumTransport.REPLY) { + const id = transport.id; + if (this.waitingReplies[id]) { + this.waitingReplies[id](transport.message.data); + delete this.waitingReplies[id]; + } + return; + } Log.warn(TAG + "onMessage need to be implemented for namespace " + this.name); this.sendReply(transport.id, transport.src, {params: {code: EnumError.NO_IMPLEMENTATION}}); } @@ -89,7 +97,10 @@ export class Channel { const message = new Transport(UUID, dst, EnumTransport.COMMAND, Channel.sequenceMessage++, new TransportMessage(this.name, data)); Log.debug(TAG + "sendCommand : " + JSON.stringify(message)); - this.sendMessage(message); + return new Promise((resolve, reject) => { + this.waitingReplies[message.id] = resolve; + this.sendMessage(message); + }); } /** diff --git a/src/index.ts b/src/index.ts index 922be9b..cefcdff 100644 --- a/src/index.ts +++ b/src/index.ts @@ -15,21 +15,22 @@ */ import "./channel/media.notifier"; -export {EnumError} from "./type/enum.error"; -export {EnumMediaStatus} from "./type/enum.media.status"; -export {EnumMedia} from "./type/enum.media"; -export {EnumTrack} from "./type/enum.track"; -export {EnumTransferMode} from "./type/enum.transfermode"; -export {Channel} from "./channel/channel"; -export {MediaChannel} from "./channel/media.channel"; -export {WebappChannel} from "./channel/webapp.channel"; -export {Media} from "./media/media"; -export {IMediaNotifier} from "./channel/media.notifier"; -export {VideoMedia} from "./media/video.media"; -export {ImageMedia} from "./media/image.media"; -export {PlaybackStatus} from "./protocol/playback.status"; -export {Metadata} from "./protocol/metadata"; -export {VideoPlaybackStatus} from "./media/video.playback.status"; -export {Logger} from "./util/logger"; -export {OCast} from "./ocast"; +export { EnumError } from "./type/enum.error"; +export { EnumMediaStatus } from "./type/enum.media.status"; +export { EnumMedia } from "./type/enum.media"; +export { EnumTrack } from "./type/enum.track"; +export { EnumTransferMode } from "./type/enum.transfermode"; +export { EnumTransport } from "./type/enum.transport"; +export { Channel } from "./channel/channel"; +export { MediaChannel } from "./channel/media.channel"; +export { WebappChannel } from "./channel/webapp.channel"; +export { Media } from "./media/media"; +export { IMediaNotifier } from "./channel/media.notifier"; +export { VideoMedia } from "./media/video.media"; +export { ImageMedia } from "./media/image.media"; +export { PlaybackStatus } from "./protocol/playback.status"; +export { Metadata } from "./protocol/metadata"; +export { VideoPlaybackStatus } from "./media/video.playback.status"; +export { Logger } from "./util/logger"; +export { OCast } from "./ocast"; export { Transport } from "./protocol/transport"; diff --git a/src/ocast.ts b/src/ocast.ts index 88bb62a..1d67a1f 100755 --- a/src/ocast.ts +++ b/src/ocast.ts @@ -104,6 +104,13 @@ export class OCast { public getChannel(service: string): Channel { return this.channels[service] as Channel; } + /** + * Initialize MediaChannel + * @private + */ + public setupChannel(channel: Channel): void { + this.channels[channel.name] = channel; + } /** * Initialize MediaChannel * @private