|
1 | | -import { HttpSubscriber, RedisSubscriber } from './subscribers'; |
| 1 | +import { HttpSubscriber, RedisSubscriber, Subscriber } from './subscribers'; |
2 | 2 | import { Channel } from './channels'; |
3 | 3 | import { Server } from './server'; |
4 | 4 | import { HttpApi } from './api'; |
@@ -35,6 +35,10 @@ export class EchoServer { |
35 | 35 | sslKeyPath: '', |
36 | 36 | sslCertChainPath: '', |
37 | 37 | sslPassphrase: '', |
| 38 | + subscribers: { |
| 39 | + http: true, |
| 40 | + redis: true |
| 41 | + }, |
38 | 42 | apiOriginAllow: { |
39 | 43 | allowCors: false, |
40 | 44 | allowOrigin: '', |
@@ -65,18 +69,11 @@ export class EchoServer { |
65 | 69 | private channel: Channel; |
66 | 70 |
|
67 | 71 | /** |
68 | | - * Redis subscriber instance. |
69 | | - * |
70 | | - * @type {RedisSubscriber} |
71 | | - */ |
72 | | - private redisSub: RedisSubscriber; |
73 | | - |
74 | | - /** |
75 | | - * Http subscriber instance. |
| 72 | + * Subscribers |
76 | 73 | * |
77 | | - * @type {HttpSubscriber} |
| 74 | + * @type {Subscriber[]} |
78 | 75 | */ |
79 | | - private httpSub: HttpSubscriber; |
| 76 | + private subscribers: Subscriber[]; |
80 | 77 |
|
81 | 78 | /** |
82 | 79 | * Http api instance. |
@@ -119,8 +116,13 @@ export class EchoServer { |
119 | 116 | init(io: any): Promise<any> { |
120 | 117 | return new Promise((resolve, reject) => { |
121 | 118 | this.channel = new Channel(io, this.options); |
122 | | - this.redisSub = new RedisSubscriber(this.options); |
123 | | - this.httpSub = new HttpSubscriber(this.server.express, this.options); |
| 119 | + |
| 120 | + this.subscribers = []; |
| 121 | + if (this.options.subscribers.http) |
| 122 | + this.subscribers.push(new HttpSubscriber(this.server.express, this.options)); |
| 123 | + if (this.options.subscribers.redis) |
| 124 | + this.subscribers.push(new RedisSubscriber(this.options)); |
| 125 | + |
124 | 126 | this.httpApi = new HttpApi(io, this.channel, this.server.express, this.options.apiOriginAllow); |
125 | 127 | this.httpApi.init(); |
126 | 128 |
|
@@ -152,15 +154,13 @@ export class EchoServer { |
152 | 154 | */ |
153 | 155 | listen(): Promise<any> { |
154 | 156 | return new Promise((resolve, reject) => { |
155 | | - let http = this.httpSub.subscribe((channel, message) => { |
156 | | - return this.broadcast(channel, message); |
157 | | - }); |
158 | | - |
159 | | - let redis = this.redisSub.subscribe((channel, message) => { |
160 | | - return this.broadcast(channel, message); |
| 157 | + let subscribePromises = this.subscribers.map(subscriber => { |
| 158 | + return subscriber.subscribe((channel, message) => { |
| 159 | + return this.broadcast(channel, message); |
| 160 | + }); |
161 | 161 | }); |
162 | 162 |
|
163 | | - Promise.all([http, redis]).then(() => resolve()); |
| 163 | + Promise.all(subscribePromises).then(() => resolve()); |
164 | 164 | }); |
165 | 165 | } |
166 | 166 |
|
|
0 commit comments