Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import * as Ably from 'ably';
- import { Queue } from 'queue-typescript';
- export interface IMessageBusMessage
- {
- topic: string;
- message: string;
- }
- export interface IMessageBus
- {
- connect(apikey : string);
- joinChannel(channelName : string)
- sendMessage(topic: string, message: string);
- }
- export class MessageBusMessage implements IMessageBusMessage
- {
- constructor(public topic: string, public message : string)
- {
- }
- }
- export class MessageBusManager implements IMessageBus
- {
- MessageQueue:Queue<MessageBusMessage>;
- constructor()
- {
- this.MessageQueue = new Queue<MessageBusMessage>();
- }
- connect(apikey: string) {
- throw new Error("Method not implemented.");
- }
- joinChannel(channelName: string) {
- throw new Error("Method not implemented.");
- }
- sendMessage(topic: string, message: string) {
- throw new Error("Method not implemented.");
- }
- }
- export class AblyMessageBusManager extends MessageBusManager
- {
- Client:Ably.Realtime;
- Channel:Ably.Types.RealtimeChannelCallbacks;
- connect(apikey: string)
- {
- let options : Ably.Types.ClientOptions = { key: apikey };
- this.Client = new Ably.Realtime(options); /* inferred type Ably.Realtime */
- }
- joinChannel(channelName: string)
- {
- this.Channel = this.Client.channels.get(channelName); /* inferred type Ably.Types.RealtimeChannel */
- this.Channel.subscribe(function(message)
- {
- let msg = new MessageBusMessage(message.name,message.data);
- this.MessageQueue.enqueue(msg);
- });
- }
- sendMessage(topic: string, message: string)
- {
- if (this.Channel != null)
- {
- this.Channel.publish(topic,message, function(err) {
- if(err) {
- throw new ErrorEvent('publish failed with error ' + err);
- } else {
- console.log('publish succeeded');
- }
- })
- }
- throw new Error("Method not implemented.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement