Advertisement
GlobalLiquidity

Untitled

Mar 15th, 2019
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import * as Ably from 'ably';
  2. import { Queue } from 'queue-typescript';
  3.  
  4. export interface IMessageBusMessage
  5. {
  6.     topic: string;
  7.     message: string;
  8. }
  9.  
  10. export interface IMessageBus
  11. {
  12.     connect(apikey : string);
  13.     joinChannel(channelName : string)
  14.     sendMessage(topic: string, message: string);
  15. }
  16.  
  17. export class MessageBusMessage implements IMessageBusMessage
  18. {
  19.     constructor(public topic: string, public message : string)
  20.     {
  21.  
  22.     }
  23. }
  24.  
  25. export class MessageBusManager implements IMessageBus
  26. {
  27.     MessageQueue:Queue<MessageBusMessage>;
  28.  
  29.     constructor()
  30.     {
  31.         this.MessageQueue = new Queue<MessageBusMessage>();
  32.     }
  33.  
  34.     connect(apikey: string) {
  35.         throw new Error("Method not implemented.");
  36.     }
  37.    
  38.     joinChannel(channelName: string) {
  39.         throw new Error("Method not implemented.");
  40.     }
  41.  
  42.     sendMessage(topic: string, message: string) {
  43.         throw new Error("Method not implemented.");
  44.     }  
  45. }
  46.  
  47. export class AblyMessageBusManager extends MessageBusManager
  48. {
  49.     Client:Ably.Realtime;
  50.     Channel:Ably.Types.RealtimeChannelCallbacks;
  51.    
  52.     connect(apikey: string)
  53.     {
  54.         let options : Ably.Types.ClientOptions = { key: apikey };
  55.         this.Client = new Ably.Realtime(options); /* inferred type Ably.Realtime */
  56.     }
  57.    
  58.     joinChannel(channelName: string)
  59.     {
  60.         this.Channel = this.Client.channels.get(channelName); /* inferred type Ably.Types.RealtimeChannel */
  61.         this.Channel.subscribe(function(message)
  62.         {
  63.             let msg = new MessageBusMessage(message.name,message.data);
  64.             this.MessageQueue.enqueue(msg);            
  65.         });    
  66.     }
  67.  
  68.     sendMessage(topic: string, message: string)
  69.     {
  70.         if (this.Channel != null)
  71.         {
  72.             this.Channel.publish(topic,message, function(err) {
  73.                 if(err) {
  74.                   throw new ErrorEvent('publish failed with error ' + err);
  75.                 } else {
  76.                   console.log('publish succeeded');
  77.                 }
  78.               })
  79.         }
  80.  
  81.         throw new Error("Method not implemented.");
  82.     }    
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement