-
Notifications
You must be signed in to change notification settings - Fork 0
/
relay_subsriber.js
120 lines (120 loc) · 4.72 KB
/
relay_subsriber.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const WsSubscribers = {
__subscribers: {},
websocket: undefined,
webSocketConnected: false,
registerQueue: [],
init: function(port, debug, debugFilters) {
port = port || 49322;
debug = false;
if (debug) {
if (debugFilters !== undefined) {
console.warn("WebSocket Debug Mode enabled with filtering. Only events not in the filter list will be dumped");
} else {
console.warn("WebSocket Debug Mode enabled without filters applied. All events will be dumped to console");
console.warn("To use filters, pass in an array of 'channel:event' strings to the second parameter of the init function");
}
}
WsSubscribers.webSocket = new WebSocket("ws://localhost:" + port);
WsSubscribers.webSocket.onmessage = function (event) {
let jEvent = JSON.parse(event.data);
if (!jEvent.hasOwnProperty('event')) {
return;
}
let eventSplit = jEvent.event.split(':');
let channel = eventSplit[0];
let event_event = eventSplit[1];
if (debug) {
if (!debugFilters) {
console.log(channel, event_event, jEvent);
} else if (debugFilters && debugFilters.indexOf(jEvent.event) < 0) {
console.log(channel, event_event, jEvent);
}
}
WsSubscribers.triggerSubscribers(channel, event_event, jEvent.data);
};
WsSubscribers.webSocket.onopen = function () {
WsSubscribers.triggerSubscribers("ws", "open");
WsSubscribers.webSocketConnected = true;
WsSubscribers.registerQueue.forEach((r) => {
WsSubscribers.send("wsRelay", "register", r);
});
WsSubscribers.registerQueue = [];
};
WsSubscribers.webSocket.onerror = function () {
WsSubscribers.triggerSubscribers("ws", "error");
WsSubscribers.webSocketConnected = false;
};
WsSubscribers.webSocket.onclose = function () {
WsSubscribers.triggerSubscribers("ws", "close");
WsSubscribers.webSocketConnected = false;
};
},
/**
* Add callbacks for when certain events are thrown
* Execution is guaranteed to be in First In First Out order
* @param channels
* @param events
* @param callback
*/
subscribe: function(channels, events, callback) {
if (typeof channels === "string") {
let channel = channels;
channels = [];
channels.push(channel);
}
if (typeof events === "string") {
let event = events;
events = [];
events.push(event);
}
channels.forEach(function(c) {
events.forEach(function (e) {
if (!WsSubscribers.__subscribers.hasOwnProperty(c)) {
WsSubscribers.__subscribers[c] = {};
}
if (!WsSubscribers.__subscribers[c].hasOwnProperty(e)) {
WsSubscribers.__subscribers[c][e] = [];
if (WsSubscribers.webSocketConnected) {
WsSubscribers.send("wsRelay", "register", `${c}:${e}`);
} else {
WsSubscribers.registerQueue.push(`${c}:${e}`);
}
}
WsSubscribers.__subscribers[c][e].push(callback);
});
})
},
clearEventCallbacks: function (channel, event) {
if (WsSubscribers.__subscribers.hasOwnProperty(channel) && WsSubscribers.__subscribers[channel].hasOwnProperty(event)) {
WsSubscribers.__subscribers[channel] = {};
}
},
triggerSubscribers: function (channel, event, data) {
if (WsSubscribers.__subscribers.hasOwnProperty(channel) && WsSubscribers.__subscribers[channel].hasOwnProperty(event)) {
WsSubscribers.__subscribers[channel][event].forEach(function(callback) {
if (callback instanceof Function) {
callback(data);
}
});
}
},
send: function (channel, event, data) {
if (typeof channel !== 'string') {
console.error("Channel must be a string");
return;
}
if (typeof event !== 'string') {
console.error("Event must be a string");
return;
}
if (channel === 'local') {
this.triggerSubscribers(channel, event, data);
} else {
let cEvent = channel + ":" + event;
WsSubscribers.webSocket.send(JSON.stringify({
'event': cEvent,
'data': data
}));
}
}
};