This repository was archived by the owner on May 1, 2020. It is now read-only.
File tree 1 file changed +15
-5
lines changed
1 file changed +15
-5
lines changed Original file line number Diff line number Diff line change @@ -15,16 +15,22 @@ export function createNotificationServer(config: ServeConfig) {
15
15
// queue up all messages to the client
16
16
function queueMessageSend ( msg : WsMessage ) {
17
17
msgToClient . push ( msg ) ;
18
- drainMessageQueue ( ) ;
18
+ drainMessageQueue ( {
19
+ broadcast : true
20
+ } ) ;
19
21
}
20
22
21
23
// drain the queue messages when the server is ready
22
- function drainMessageQueue ( ) {
23
- if ( wsServer ) {
24
+ function drainMessageQueue ( options = { broadcast : false } ) {
25
+ let sendMethod = wsServer . send ;
26
+ if ( options . hasOwnProperty ( 'broadcast' ) && options . broadcast ) {
27
+ sendMethod = wss . broadcast ;
28
+ }
29
+ if ( wss . clients . length > 0 ) {
24
30
let msg : any ;
25
31
while ( msg = msgToClient . shift ( ) ) {
26
32
try {
27
- wsServer . send ( JSON . stringify ( msg ) ) ;
33
+ sendMethod ( JSON . stringify ( msg ) ) ;
28
34
} catch ( e ) {
29
35
if ( e . message !== 'not opened' ) {
30
36
Logger . error ( `error sending client ws - ${ e . message } ` ) ;
@@ -64,7 +70,11 @@ export function createNotificationServer(config: ServeConfig) {
64
70
65
71
// create web socket server
66
72
const wss = new WebSocketServer ( { port : config . notificationPort } ) ;
67
-
73
+ wss . broadcast = function broadcast ( data : any ) {
74
+ wss . clients . forEach ( function each ( client : any ) {
75
+ client . send ( data ) ;
76
+ } ) ;
77
+ } ;
68
78
wss . on ( 'connection' , ( ws : any ) => {
69
79
// we've successfully connected
70
80
wsServer = ws ;
You can’t perform that action at this time.
0 commit comments