Skip to content
This repository was archived by the owner on Nov 2, 2020. It is now read-only.

Commit f63b539

Browse files
authored
Merge pull request #19 from ClusterWS/next
Next
2 parents a921d50 + bd93445 commit f63b539

13 files changed

Lines changed: 308 additions & 250 deletions

File tree

dist/browser/ClusterWS.js

Lines changed: 78 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -36,65 +36,84 @@
3636
Object.defineProperty(t, "__esModule", {
3737
value: !0
3838
});
39-
var o = n(1), s = n(2), i = n(3), r = n(4), c = n(5), u = function() {
39+
var o = n(1), s = n(2), r = n(3), i = n(4), c = n(5), u = function() {
4040
function e(e) {
41-
return e.url ? e.port ? (this.options = {
41+
return this.channels = {}, this.events = new s.EventEmitter(), this.missedPing = 0,
42+
this.useBinary = !1, e.url && "string" == typeof e.url ? e.port && "number" == typeof e.port ? (this.options = {
4243
url: e.url,
4344
port: e.port,
4445
autoReconnect: e.autoReconnect || !1,
4546
reconnectionIntervalMin: e.reconnectionIntervalMin || 1e3,
4647
reconnectionIntervalMax: e.reconnectionIntervalMax || 5e3,
4748
reconnectionAttempts: e.reconnectionAttempts || 0
48-
}, this.options.reconnectionIntervalMin > this.options.reconnectionIntervalMax ? void o.logError("Min reconnection interval can not be more then Max reconnection interval") : (this.lost = 0,
49-
this.events = new s.EventEmitter(), this.channels = {}, this.reconnection = new c.Reconnect(this),
50-
void this.create())) : void o.logError("Port must be provided") : void o.logError("Url must be provided");
49+
}, this.options.reconnectionIntervalMin > this.options.reconnectionIntervalMax ? i.logError("reconnectionIntervalMin can not be more then reconnectionIntervalMax") : (this.reconnection = new r.Reconnection(this),
50+
void this.create())) : i.logError("Port must be provided and it must be number") : i.logError("Url must be provided and it must be string");
5151
}
52-
return e.prototype.create = function() {
52+
return e.getBuffer = function(e) {
53+
for (var t = new Uint8Array(e.length), n = 0, o = e.length; n < o; n++) t[n] = e.charCodeAt(n);
54+
return t.buffer;
55+
}, e.prototype.create = function() {
5356
var e = this;
5457
this.websocket = new WebSocket("ws://" + this.options.url + ":" + this.options.port),
55-
this.websocket.onopen = function() {
56-
e.reconnection.isConnected(), e.events.emit("connect");
58+
this.websocket.binaryType = "arraybuffer", this.websocket.onopen = function() {
59+
return e.reconnection.isConnected();
60+
}, this.websocket.onerror = function() {
61+
return e.events.emit("error");
5762
}, this.websocket.onmessage = function(t) {
58-
if ("#0" === t.data) return e.send("#1", null, "ping"), e.lost = 0;
63+
if (t = t.data, e.useBinary && "string" != typeof t && (t = String.fromCharCode.apply(null, new Uint8Array(t))),
64+
"#0" === t) return e.send("#1", null, "ping"), e.missedPing = 0;
5965
try {
60-
t = JSON.parse(t.data);
66+
t = JSON.parse(t);
6167
} catch (e) {
62-
return o.logError(e);
68+
return i.logError(e);
6369
}
64-
i.socketDecodeMessages(e, t);
70+
c.socketDecodeMessages(e, t);
6571
}, this.websocket.onclose = function(t) {
66-
if (e.lost = 0, clearInterval(e.pingInterval), e.events.emit("disconnect", t.code, t.reason),
72+
if (e.missedPing = 0, clearInterval(e.pingInterval), e.events.emit("disconnect", t.code, t.reason),
6773
!e.reconnection.inReconnectionState) {
6874
if (e.options.autoReconnect && 1e3 !== t.code) return e.reconnection.reconnect();
6975
e.events.removeAllEvents();
7076
for (var n in e) e.hasOwnProperty(n) && delete e[n];
7177
}
72-
}, this.websocket.onerror = function() {
73-
return e.events.emit("error");
7478
};
7579
}, e.prototype.on = function(e, t) {
7680
this.events.on(e, t);
77-
}, e.prototype.send = function(e, t, n) {
78-
this.websocket.send(i.socketEncodeMessages(e, t, n || "emit"));
81+
}, e.prototype.send = function(t, n, o) {
82+
if (this.useBinary) return this.websocket.send(e.getBuffer(c.socketEncodeMessages(t, n, o || "emit")));
83+
this.websocket.send(c.socketEncodeMessages(t, n, o || "emit"));
7984
}, e.prototype.disconnect = function(e, t) {
8085
this.websocket.close(e || 1e3, t);
81-
}, e.prototype.subscribe = function(e) {
82-
return this.channels[e] ? this.channels[e] : this.channels[e] = new r.Channel(e, this);
8386
}, e.prototype.getState = function() {
8487
return this.websocket.readyState;
88+
}, e.prototype.subscribe = function(e) {
89+
return this.channels[e] ? this.channels[e] : this.channels[e] = new o.Channel(e, this);
8590
}, e.prototype.getChannelByName = function(e) {
8691
return this.channels[e];
8792
}, e;
8893
}();
8994
t.ClusterWS = u;
9095
}, function(e, t, n) {
9196
"use strict";
92-
function o(e) {
93-
console.log(e);
94-
}
9597
Object.defineProperty(t, "__esModule", {
9698
value: !0
97-
}), t.logError = o;
99+
});
100+
var o = function() {
101+
function e(e, t) {
102+
this.channel = e, this.socket = t, this.subscribe();
103+
}
104+
return e.prototype.watch = function(e) {
105+
return this.listener = e, this;
106+
}, e.prototype.publish = function(e) {
107+
return this.socket.send(this.channel, e, "publish"), this;
108+
}, e.prototype.unsubscribe = function() {
109+
this.socket.send("unsubscribe", this.channel, "system"), this.socket.channels[this.channel] = null;
110+
}, e.prototype.onMessage = function(e) {
111+
this.listener && this.listener.call(null, e);
112+
}, e.prototype.subscribe = function() {
113+
this.socket.send("subscribe", this.channel, "system");
114+
}, e;
115+
}();
116+
t.Channel = o;
98117
}, function(e, t, n) {
99118
"use strict";
100119
Object.defineProperty(t, "__esModule", {
@@ -115,6 +134,40 @@
115134
}, e;
116135
}();
117136
t.EventEmitter = o;
137+
}, function(e, t, n) {
138+
"use strict";
139+
Object.defineProperty(t, "__esModule", {
140+
value: !0
141+
});
142+
var o = function() {
143+
function e(e) {
144+
this.socket = e, this.inReconnectionState = !1, this.reconnectionAttempted = 0,
145+
this.autoReconnect = this.socket.options.autoReconnect;
146+
}
147+
return e.prototype.isConnected = function() {
148+
clearTimeout(this.timer), clearInterval(this.interval), this.inReconnectionState = !1,
149+
this.reconnectionAttempted = 0;
150+
for (var e in this.socket.channels) this.socket.channels.hasOwnProperty(e) && this.socket.channels[e].subscribe();
151+
}, e.prototype.reconnect = function() {
152+
var e = this;
153+
this.inReconnectionState = !0, this.interval = setInterval(function() {
154+
e.socket.websocket.readyState === e.socket.websocket.CLOSED && (e.reconnectionAttempted++,
155+
0 !== e.socket.options.reconnectionAttempts && e.reconnectionAttempted >= e.socket.options.reconnectionAttempts && (clearInterval(e.interval),
156+
e.autoReconnect = !1, e.inReconnectionState = !1), clearTimeout(e.timer), e.timer = setTimeout(function() {
157+
return e.socket.create();
158+
}, Math.floor(Math.random() * (e.socket.options.reconnectionIntervalMax - e.socket.options.reconnectionIntervalMin + 1))));
159+
}, this.socket.options.reconnectionIntervalMin);
160+
}, e;
161+
}();
162+
t.Reconnection = o;
163+
}, function(e, t, n) {
164+
"use strict";
165+
function o(e) {
166+
return console.log(e);
167+
}
168+
Object.defineProperty(t, "__esModule", {
169+
value: !0
170+
}), t.logError = o;
118171
}, function(e, t, n) {
119172
"use strict";
120173
function o(e, t, n) {
@@ -163,62 +216,13 @@
163216
switch (t["#"][1]) {
164217
case "c":
165218
e.pingInterval = setInterval(function() {
166-
return e.lost < 3 ? e.lost++ : e.disconnect(4001, "Did not get pings");
167-
}, t["#"][2].ping);
219+
return e.missedPing < 3 ? e.missedPing++ : e.disconnect(4001, "Did not get pings");
220+
}, t["#"][2].ping), e.useBinary = t["#"][2].binary, e.events.emit("connect");
168221
}
169222
}
170223
}
171224
Object.defineProperty(t, "__esModule", {
172225
value: !0
173226
}), t.socketEncodeMessages = o, t.socketDecodeMessages = s;
174-
}, function(e, t, n) {
175-
"use strict";
176-
Object.defineProperty(t, "__esModule", {
177-
value: !0
178-
});
179-
var o = function() {
180-
function e(e, t) {
181-
this.channel = e, this.socket = t, this.subscribe();
182-
}
183-
return e.prototype.watch = function(e) {
184-
return this.listener = e, this;
185-
}, e.prototype.publish = function(e) {
186-
return this.socket.send(this.channel, e, "publish"), this;
187-
}, e.prototype.unsubscribe = function() {
188-
this.socket.send("unsubscribe", this.channel, "system"), this.socket.channels[this.channel] = null;
189-
}, e.prototype.onMessage = function(e) {
190-
this.listener && this.listener.call(null, e);
191-
}, e.prototype.subscribe = function() {
192-
this.socket.send("subscribe", this.channel, "system");
193-
}, e;
194-
}();
195-
t.Channel = o;
196-
}, function(e, t, n) {
197-
"use strict";
198-
Object.defineProperty(t, "__esModule", {
199-
value: !0
200-
});
201-
var o = function() {
202-
function e(e) {
203-
this.socket = e, this.autoReconnect = this.socket.options.autoReconnect, this.inReconnectionState = !1,
204-
this.reconnectionAttempted = 0;
205-
}
206-
return e.prototype.isConnected = function() {
207-
clearTimeout(this.timer), clearInterval(this.interval), this.inReconnectionState = !1,
208-
this.reconnectionAttempted = 0;
209-
var e = this.socket.channels;
210-
for (var t in e) e.hasOwnProperty(t) && e[t].subscribe();
211-
}, e.prototype.reconnect = function() {
212-
var e = this;
213-
this.inReconnectionState = !0, this.interval = setInterval(function() {
214-
e.socket.websocket.readyState === e.socket.websocket.CLOSED && (e.reconnectionAttempted++,
215-
0 !== e.socket.options.reconnectionAttempts && e.reconnectionAttempted >= e.socket.options.reconnectionAttempts && (clearInterval(e.interval),
216-
e.autoReconnect = !1, e.inReconnectionState = !1), clearTimeout(e.timer), e.timer = setTimeout(function() {
217-
e.socket.create();
218-
}, Math.floor(Math.random() * (e.socket.options.reconnectionIntervalMax - e.socket.options.reconnectionIntervalMin + 1))));
219-
}, this.socket.options.reconnectionIntervalMin);
220-
}, e;
221-
}();
222-
t.Reconnect = o;
223227
} ]);
224228
});

0 commit comments

Comments
 (0)