forked from wilk/ExtJS-WebSocket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebSocketManager.js
207 lines (185 loc) · 6.14 KB
/
WebSocketManager.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/**
* @class Ext.ux.WebSocketManager
* @author Vincenzo Ferrari <[email protected]>
* @singleton
*
* Manager of Ext.ux.WebSocket
*
* This singleton provide some useful functions to use for many websockets.
*
* var ws1 = Ext.create ('Ext.ux.WebSocket', {
* url: 'ws://localhost:8888'
* });
*
* Ext.ux.WebSocketManager.register (ws1);
*
* var ws2 = Ext.create ('Ext.ux.WebSocket', {
* url: 'ws://localhost:8900'
* });
*
* Ext.ux.WebSocketManager.register (ws2);
*
* var ws3 = Ext.create ('Ext.ux.WebSocket', {
* url: 'ws://localhost:8950'
* });
*
* Ext.ux.WebSocketManager.register (ws3);
*
* Ext.ux.WebSocketManager.listen ('system shutdown', function (ws, data) {
* Ext.Msg.show ({
* title: 'System Shutdown' ,
* msg: data ,
* icon: Ext.Msg.WARNING ,
* buttons: Ext.Msg.OK
* });
* });
*
* Ext.ux.WebSocketManager.broadcast ('system shutdown', 'BROADCAST: the system will shutdown in few minutes.');
*
* Ext.ux.WebSocketManager.closeAll ();
*
* Ext.ux.WebSocketManager.unregister (ws1);
* Ext.ux.WebSocketManager.unregister (ws2);
* Ext.ux.WebSocketManager.unregister (ws3);
*/
Ext.define('Ext.ux.WebSocketManager', {
singleton: true,
/**
* @property {Ext.util.HashMap} wsList
* @private
*/
wsList: Ext.create('Ext.util.HashMap'),
/**
* @method register
* Registers one or more Ext.ux.WebSocket
* @param {Ext.ux.WebSocket/Ext.ux.WebSocket[]} websockets WebSockets to register. Could be only one.
*/
register: function (websockets) {
var me = this;
// Changes websockets into an array in every case
if (Ext.isObject(websockets)) websockets = [websockets];
Ext.each(websockets, function (websocket) {
if (!Ext.isEmpty(websocket.url)) me.wsList.add(websocket.url, websocket);
});
},
/**
* @method contains
* Checks if a websocket is already registered or not
* @param {Ext.ux.WebSocket} websocket The WebSocket to find
* @return {Boolean} True if the websocket is already registered, False otherwise
*/
contains: function (websocket) {
return this.wsList.containsKey(websocket.url);
},
/**
* @method get
* Retrieves a registered websocket by its url
* @param {String} url The url of the websocket to search
* @return {Ext.ux.WebSocket} The websocket or undefined
*/
get: function (url) {
return this.wsList.get(url);
},
/**
* @method each
* Executes a function for each registered websocket
* @param {Function} fn The function to execute
*/
each: function (fn) {
this.wsList.each(function (url, websocket, len) {
fn(websocket);
});
},
/**
* @method unregister
* Unregisters one or more Ext.ux.WebSocket
* @param {Ext.ux.WebSocket/Ext.ux.WebSocket[]} websockets WebSockets to unregister
*/
unregister: function (websockets) {
var me = this;
if (Ext.isObject(websockets)) websockets = [websockets];
Ext.each(websockets, function (websocket) {
if (me.wsList.containsKey(websocket.url)) me.wsList.removeAtKey(websocket.url);
});
},
/**
* @method broadcast
* Sends a message to each websocket
* @param {String} event The event to raise
* @param {String/Object} message The data to send
*/
broadcast: function (event, message) {
this.multicast([], event, message);
},
/**
* @method multicast
* Sends a message to each websocket, except those specified
* @param {Ext.ux.WebSocket/Ext.ux.WebSocket[]} websockets An array of websockets to take off the communication
* @param {String} event The event to raise
* @param {String/Object} data The data to send
*/
multicast: function (websockets, event, data) {
this.getExcept(websockets).each(function (url, websocket, len) {
if (websocket.isReady()) {
if (Ext.isEmpty(data)) websocket.send(event);
else websocket.send(event, data);
}
});
},
/**
* @method listen
* Adds an handler for events given to each registered websocket
* @param {String/String[]} events Events to listen
* @param {Function} handler The events' handler
*/
listen: function (events, handler) {
if (Ext.isString(events)) events = [events];
this.wsList.each(function (url, websocket, len) {
Ext.each(events, function (event) {
websocket.on(event, handler);
});
});
},
/**
* @method listenExcept
* Adds an handler for events given to each registered websocket, except websockets given
* @param {String/String[]} events Events to listen
* @param {Ext.ux.WebSocket/Ext.ux.WebSocket[]} websockets WebSockets to exclude
* @param {Function} handler The events' handler
*/
listenExcept: function (events, websockets, handler) {
if (Ext.isString(events)) events = [events];
this.getExcept(websockets).each(function (url, websocket, len) {
Ext.each(events, function (event) {
websocket.on(event, handler);
});
});
},
/**
* @method getExcept
* Retrieves registered websockets except the input
* @param {Ext.ux.WebSocket/Ext.ux.WebSocket[]} websockets WebSockets to exclude
* @return {Ext.util.HashMap} Registered websockets except the input
* @private
*/
getExcept: function (websockets) {
if (Ext.isObject(websockets)) websockets = [websockets];
var list = this.wsList.clone();
// Exclude websockets from the communication
Ext.each(websockets, function (websocket) {
list.removeAtKey(websocket.url);
});
return list;
},
/**
* @method closeAll
* Closes any registered websocket
*/
closeAll: function () {
var me = this;
me.wsList.each(function (url, websocket, len) {
websocket.close();
me.unregister(websocket);
});
}
});