|
1 | 1 | var ClusterWS = function() { |
2 | 2 | "use strict"; |
3 | | - function n(n) { |
4 | | - return console.log(n); |
| 3 | + function t(t) { |
| 4 | + return console.log(t); |
5 | 5 | } |
6 | | - var t = function() { |
7 | | - function t(n, t) { |
8 | | - this.socket = n, this.name = t, this.subscribe(); |
| 6 | + var n = function() { |
| 7 | + function n(t, n) { |
| 8 | + this.name = n, this.socket = t, this.subscribe(); |
9 | 9 | } |
10 | | - return t.prototype.watch = function(t) { |
11 | | - return "[object Function]" !== {}.toString.call(t) ? n("Listener must be a function") : (this.listener = t, |
| 10 | + return n.prototype.watch = function(n) { |
| 11 | + return "[object Function]" !== {}.toString.call(n) ? t("Listener must be a function") : (this.listener = n, |
12 | 12 | this); |
13 | | - }, t.prototype.publish = function(n) { |
14 | | - return this.socket.send(this.name, n, "publish"), this; |
15 | | - }, t.prototype.unsubscribe = function() { |
| 13 | + }, n.prototype.publish = function(t) { |
| 14 | + return this.socket.send(this.name, t, "publish"), this; |
| 15 | + }, n.prototype.unsubscribe = function() { |
16 | 16 | this.socket.send("unsubscribe", this.name, "system"), this.socket.channels[this.name] = null; |
17 | | - }, t.prototype.onMessage = function(n) { |
18 | | - this.listener && this.listener.call(null, n); |
19 | | - }, t.prototype.subscribe = function() { |
| 17 | + }, n.prototype.onMessage = function(t) { |
| 18 | + this.listener && this.listener.call(null, t); |
| 19 | + }, n.prototype.subscribe = function() { |
20 | 20 | this.socket.send("subscribe", this.name, "system"); |
21 | | - }, t; |
| 21 | + }, n; |
22 | 22 | }(), e = function() { |
23 | | - function t() { |
| 23 | + function n() { |
24 | 24 | this.events = {}; |
25 | 25 | } |
26 | | - return t.prototype.on = function(t, e) { |
27 | | - if ("[object Function]" !== {}.toString.call(e)) return n("Listener must be a function"); |
28 | | - this.events[t] = e; |
29 | | - }, t.prototype.emit = function(n) { |
30 | | - for (var t, e = [], o = 1; o < arguments.length; o++) e[o - 1] = arguments[o]; |
31 | | - this.events[n] && (t = this.events[n]).call.apply(t, [ null ].concat(e)); |
32 | | - }, t.prototype.removeAllEvents = function() { |
| 26 | + return n.prototype.on = function(n, e) { |
| 27 | + if ("[object Function]" !== {}.toString.call(e)) return t("Listener must be a function"); |
| 28 | + this.events[n] = e; |
| 29 | + }, n.prototype.emit = function(t) { |
| 30 | + for (var n, e = [], o = 1; o < arguments.length; o++) e[o - 1] = arguments[o]; |
| 31 | + this.events[t] && (n = this.events[t]).call.apply(n, [ null ].concat(e)); |
| 32 | + }, n.prototype.removeAllEvents = function() { |
33 | 33 | this.events = {}; |
34 | | - }, t; |
| 34 | + }, n; |
35 | 35 | }(); |
36 | | - function o(n, t, e) { |
| 36 | + function o(t, n, e) { |
37 | 37 | var o = { |
38 | 38 | emit: { |
39 | | - "#": [ "e", n, t ] |
| 39 | + "#": [ "e", t, n ] |
40 | 40 | }, |
41 | 41 | publish: { |
42 | | - "#": [ "p", n, t ] |
| 42 | + "#": [ "p", t, n ] |
43 | 43 | }, |
44 | 44 | system: { |
45 | 45 | subscribe: { |
46 | | - "#": [ "s", "s", t ] |
| 46 | + "#": [ "s", "s", n ] |
47 | 47 | }, |
48 | 48 | unsubscribe: { |
49 | | - "#": [ "s", "u", t ] |
| 49 | + "#": [ "s", "u", n ] |
50 | 50 | } |
51 | 51 | } |
52 | 52 | }; |
53 | | - return "ping" === e ? n : JSON.stringify("system" === e ? o[e][n] : o[e]); |
| 53 | + return "ping" === e ? t : JSON.stringify("system" === e ? o[e][t] : o[e]); |
54 | 54 | } |
55 | 55 | return function() { |
56 | | - function i(t) { |
| 56 | + function s(n) { |
57 | 57 | return this.events = new e(), this.channels = {}, this.useBinary = !1, this.missedPing = 0, |
58 | | - this.inReconnection = !1, this.reconnectionAttempted = 0, t.url ? (this.options = { |
59 | | - url: t.url, |
60 | | - autoReconnect: t.autoReconnect || !1, |
61 | | - reconnectionAttempts: t.reconnectionAttempts || 0, |
62 | | - reconnectionIntervalMin: t.reconnectionIntervalMin || 1e3, |
63 | | - reconnectionIntervalMax: t.reconnectionIntervalMax || 5e3 |
64 | | - }, this.options.reconnectionIntervalMin > this.options.reconnectionIntervalMax ? n("reconnectionIntervalMin can not be more then reconnectionIntervalMax") : void this.create()) : n("Url must be provided and it must be string"); |
| 58 | + this.reconnectionAttempted = 0, n.url ? (this.options = { |
| 59 | + url: n.url, |
| 60 | + autoReconnect: n.autoReconnect || !1, |
| 61 | + autoReconnectOptions: n.autoReconnectOptions ? { |
| 62 | + attempts: n.autoReconnectOptions.attempts || 0, |
| 63 | + minInterval: n.autoReconnectOptions.minInterval || 1e3, |
| 64 | + maxInterval: n.autoReconnectOptions.maxInterval || 5e3 |
| 65 | + } : { |
| 66 | + attempts: 0, |
| 67 | + minInterval: 1e3, |
| 68 | + maxInterval: 5e3 |
| 69 | + } |
| 70 | + }, this.options.autoReconnectOptions.minInterval > this.options.autoReconnectOptions.maxInterval ? t("minInterval option can not be more than maxInterval option") : void this.create()) : t("Url must be provided and it must be string"); |
65 | 71 | } |
66 | | - return i.prototype.create = function() { |
67 | | - var t = this, e = window.MozWebSocket || window.WebSocket; |
| 72 | + return s.prototype.on = function(t, n) { |
| 73 | + this.events.on(t, n); |
| 74 | + }, s.prototype.send = function(t, n, e) { |
| 75 | + void 0 === e && (e = "emit"), this.websocket.send(this.useBinary ? function(t) { |
| 76 | + for (var n = t.length, e = new Uint8Array(n), o = 0; o < n; o++) e[o] = t.charCodeAt(o); |
| 77 | + return e.buffer; |
| 78 | + }(o(t, n, e)) : o(t, n, e)); |
| 79 | + }, s.prototype.disconnect = function(t, n) { |
| 80 | + this.websocket.close(t || 1e3, n); |
| 81 | + }, s.prototype.subscribe = function(t) { |
| 82 | + return this.channels[t] ? this.channels[t] : this.channels[t] = new n(this, t); |
| 83 | + }, s.prototype.getChannelByName = function(t) { |
| 84 | + return this.channels[t]; |
| 85 | + }, s.prototype.getState = function() { |
| 86 | + return this.websocket.readyState; |
| 87 | + }, s.prototype.create = function() { |
| 88 | + var n = this, e = window.MozWebSocket || window.WebSocket; |
68 | 89 | this.websocket = new e(this.options.url), this.websocket.binaryType = "arraybuffer", |
69 | 90 | this.websocket.onopen = function() { |
70 | | - for (var n in t.reconnectionAttempted = 0, t.channels) t.channels[n] && t.channels[n].subscribe(); |
71 | | - }, this.websocket.onerror = function(n) { |
72 | | - return t.events.emit("error", n); |
| 91 | + n.reconnectionAttempted = 0; |
| 92 | + for (var t = 0, e = Object.keys(n.channels), o = e.length; t < o; t++) n.channels[e[t]] && n.channels[e[t]].subscribe(); |
| 93 | + }, this.websocket.onerror = function(t) { |
| 94 | + return n.events.emit("error", t); |
73 | 95 | }, this.websocket.onmessage = function(e) { |
74 | 96 | var o = "string" != typeof e.data ? String.fromCharCode.apply(null, new Uint8Array(e.data)) : e.data; |
75 | | - if ("#0" === o) return t.missedPing = 0, t.send("#1", null, "ping"); |
| 97 | + if ("#0" === o) return n.missedPing = 0, n.send("#1", null, "ping"); |
76 | 98 | try { |
77 | 99 | o = JSON.parse(o); |
78 | | - } catch (t) { |
79 | | - return n(t); |
| 100 | + } catch (n) { |
| 101 | + return t(n); |
80 | 102 | } |
81 | | - !function(n, t) { |
| 103 | + !function(t, n) { |
82 | 104 | var e = { |
83 | 105 | e: function() { |
84 | | - return n.events.emit(t["#"][1], t["#"][2]); |
| 106 | + return t.events.emit(n["#"][1], n["#"][2]); |
85 | 107 | }, |
86 | 108 | p: function() { |
87 | | - return n.channels[t["#"][1]] && n.channels[t["#"][1]].onMessage(t["#"][2]); |
| 109 | + return t.channels[n["#"][1]] && t.channels[n["#"][1]].onMessage(n["#"][2]); |
88 | 110 | }, |
89 | 111 | s: { |
90 | 112 | c: function() { |
91 | | - n.pingInterval = setInterval(function() { |
92 | | - return n.missedPing++ > 2 && n.disconnect(4001, "Did not get pings"); |
93 | | - }, t["#"][2].ping), n.useBinary = t["#"][2].binary, n.events.emit("connect"); |
| 113 | + t.pingInterval = setInterval(function() { |
| 114 | + return t.missedPing++ > 2 && t.disconnect(4001, "Did not get pings"); |
| 115 | + }, n["#"][2].ping), t.useBinary = n["#"][2].binary, t.events.emit("connect"); |
94 | 116 | } |
95 | 117 | } |
96 | 118 | }; |
97 | | - "s" === t["#"][0] ? e[t["#"][0]][t["#"][1]] && e[t["#"][0]][t["#"][1]].call(null) : e[t["#"][0]] && e[t["#"][0]].call(null); |
98 | | - }(t, o); |
99 | | - }, this.websocket.onclose = function(n) { |
100 | | - if (t.missedPing = 0, clearInterval(t.pingInterval), t.events.emit("disconnect", n.code, n.reason), |
101 | | - t.options.autoReconnect && 1e3 !== n.code && (0 === t.options.reconnectionAttempts || t.reconnectionAttempted < t.options.reconnectionAttempts)) t.websocket.readyState === t.websocket.CLOSED && (t.reconnectionAttempted++, |
102 | | - t.websocket = null, setTimeout(function() { |
103 | | - return t.create(); |
104 | | - }, Math.floor(Math.random() * (t.options.reconnectionIntervalMax - t.options.reconnectionIntervalMin + 1)))); else for (var e in t.events.removeAllEvents(), |
105 | | - t) t[e] && (t[e] = null); |
| 119 | + "s" === n["#"][0] ? e[n["#"][0]][n["#"][1]] && e[n["#"][0]][n["#"][1]].call(null) : e[n["#"][0]] && e[n["#"][0]].call(null); |
| 120 | + }(n, o); |
| 121 | + }, this.websocket.onclose = function(t) { |
| 122 | + if (n.missedPing = 0, clearInterval(n.pingInterval), n.events.emit("disconnect", t.code, t.reason), |
| 123 | + n.options.autoReconnect && 1e3 !== t.code && (0 === n.options.autoReconnectOptions.attempts || n.reconnectionAttempted < n.options.autoReconnectOptions.attempts)) n.websocket.readyState === n.websocket.CLOSED ? (n.reconnectionAttempted++, |
| 124 | + n.websocket = void 0, setTimeout(function() { |
| 125 | + return n.create(); |
| 126 | + }, Math.floor(Math.random() * (n.options.autoReconnectOptions.maxInterval - n.options.autoReconnectOptions.minInterval + 1)))) : console.log("Some thing wrong with close event please contact developer"); else { |
| 127 | + n.events.removeAllEvents(); |
| 128 | + for (var e = 0, o = Object.keys(n), s = o.length; e < s; e++) n[o[e]] = null; |
| 129 | + } |
106 | 130 | }; |
107 | | - }, i.prototype.on = function(n, t) { |
108 | | - this.events.on(n, t); |
109 | | - }, i.prototype.send = function(n, t, e) { |
110 | | - void 0 === e && (e = "emit"), this.websocket.send(this.useBinary ? function(n) { |
111 | | - for (var t = n.length, e = new Uint8Array(t), o = 0; o < t; o++) e[o] = n.charCodeAt(o); |
112 | | - return e.buffer; |
113 | | - }(o(n, t, e)) : o(n, t, e)); |
114 | | - }, i.prototype.disconnect = function(n, t) { |
115 | | - this.websocket.close(n || 1e3, t); |
116 | | - }, i.prototype.subscribe = function(n) { |
117 | | - return this.channels[n] ? this.channels[n] : this.channels[n] = new t(this, n); |
118 | | - }, i.prototype.getChannelByName = function(n) { |
119 | | - return this.channels[n]; |
120 | | - }, i.prototype.getState = function() { |
121 | | - return this.websocket.readyState; |
122 | | - }, i; |
| 131 | + }, s; |
123 | 132 | }(); |
124 | 133 | }(); |
0 commit comments