Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Nov 2, 2020. It is now read-only.

Commit c8aeaf9

Browse files
committed
Add basic reconnection, add close event
1 parent f66f2bd commit c8aeaf9

File tree

5 files changed

+125
-36
lines changed

5 files changed

+125
-36
lines changed

‎dist/browser/clusterws.js‎

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,17 @@ var ClusterWSClient = (function () {
101101
autoConnect: configurations.autoConnect !== false,
102102
autoReconnect: configurations.autoReconnect || false,
103103
autoResubscribe: configurations.autoResubscribe !== false,
104-
autoReconnectOptions: {},
104+
autoReconnectOptions: {
105+
attempts: configurations.autoReconnectOptions ?
106+
configurations.autoReconnectOptions.attempts || 0 : 0,
107+
minInterval: configurations.autoReconnectOptions ?
108+
configurations.autoReconnectOptions.maxInterval || 500 : 500,
109+
maxInterval: configurations.autoReconnectOptions ?
110+
configurations.autoReconnectOptions.maxInterval || 2000 : 2000
111+
},
105112
logger: configurations.loggerOptions && configurations.loggerOptions.logger ?
106113
configurations.loggerOptions.logger :
107-
new Logger(configurations.loggerOptions && configurations.loggerOptions.level ?
108-
configurations.loggerOptions.level : LogLevel.ALL)
114+
new Logger(configurations.loggerOptions ? configurations.loggerOptions.level || LogLevel.ALL : LogLevel.ALL)
109115
};
110116
if (!this.options.url) {
111117
return this.options.logger.error('url must be provided');
@@ -115,6 +121,20 @@ var ClusterWSClient = (function () {
115121
this.connect();
116122
}
117123
}
124+
Object.defineProperty(ClusterWSClient.prototype, "OPEN", {
125+
get: function () {
126+
return this.socket.OPEN;
127+
},
128+
enumerable: true,
129+
configurable: true
130+
});
131+
Object.defineProperty(ClusterWSClient.prototype, "CLOSED", {
132+
get: function () {
133+
return this.socket.CLOSED;
134+
},
135+
enumerable: true,
136+
configurable: true
137+
});
118138
Object.defineProperty(ClusterWSClient.prototype, "readyState", {
119139
get: function () {
120140
return this.socket ? this.socket.readyState : 0;
@@ -126,8 +146,8 @@ var ClusterWSClient = (function () {
126146
get: function () {
127147
return this.socket.binaryType;
128148
},
129-
set: function (type) {
130-
this.socket.binaryType = type;
149+
set: function (binaryType) {
150+
this.socket.binaryType = binaryType;
131151
},
132152
enumerable: true,
133153
configurable: true
@@ -139,17 +159,28 @@ var ClusterWSClient = (function () {
139159
}
140160
this.isCreated = true;
141161
this.socket = new Socket(this.options.url);
142-
this.socket.binaryType = 'arraybuffer';
143162
this.socket.onopen = function () {
144163
};
145-
this.socket.onclose = function () {
164+
this.socket.onclose = function (codeEvent, reason) {
165+
_this.isCreated = false;
166+
var closeCode = typeof codeEvent === 'number' ? codeEvent : codeEvent.code;
167+
var closeReason = typeof codeEvent === 'number' ? reason : codeEvent.reason;
168+
_this.emitter.emit('close', closeCode, closeReason);
169+
if (_this.options.autoReconnect && closeCode !== 1000) {
170+
if (_this.readyState === _this.CLOSED) {
171+
return setTimeout(function () {
172+
_this.connect();
173+
}, Math.floor(Math.random() * (_this.options.autoReconnectOptions.maxInterval - _this.options.autoReconnectOptions.minInterval + 1)));
174+
}
175+
}
176+
_this.emitter.removeEvents();
146177
};
147178
this.socket.onmessage = function (message) {
148179
var messageToProcess = message;
149180
if (message.data) {
150181
messageToProcess = message.data;
151182
}
152-
_this.withPing(messageToProcess, function () {
183+
_this.parsePing(messageToProcess, function () {
153184
if (_this.emitter.exist('message')) {
154185
return _this.emitter.emit('message', messageToProcess);
155186
}
@@ -172,10 +203,10 @@ var ClusterWSClient = (function () {
172203
};
173204
ClusterWSClient.prototype.processMessage = function (message) {
174205
};
175-
ClusterWSClient.prototype.withPing = function (message, next) {
206+
ClusterWSClient.prototype.parsePing = function (message, next) {
176207
var _this = this;
177208
if (message.size === 1 || message.byteLength === 1) {
178-
var pingProcessor_1 = function (possiblePingMessage) {
209+
var parser_1 = function (possiblePingMessage) {
179210
if (new Uint8Array(possiblePingMessage)[0] === 57) {
180211
_this.socket.send(PONG);
181212
return _this.emitter.emit('ping');
@@ -184,10 +215,10 @@ var ClusterWSClient = (function () {
184215
};
185216
if (message instanceof Blob) {
186217
var reader = new FileReader();
187-
reader.onload = function (event) { return pingProcessor_1(event.srcElement.result); };
218+
reader.onload = function (event) { return parser_1(event.srcElement.result); };
188219
return reader.readAsArrayBuffer(message);
189220
}
190-
return pingProcessor_1(message);
221+
return parser_1(message);
191222
}
192223
return next();
193224
};

‎dist/browser/clusterws.min.js‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎dist/index.js‎

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,28 @@ var EventEmitter = function() {
5757
autoConnect: !1 !== e.autoConnect,
5858
autoReconnect: e.autoReconnect || !1,
5959
autoResubscribe: !1 !== e.autoResubscribe,
60-
autoReconnectOptions: {},
61-
logger: e.loggerOptions && e.loggerOptions.logger ? e.loggerOptions.logger : new Logger(e.loggerOptions && e.loggerOptions.level ? e.loggerOptions.level : LogLevel.ALL)
60+
autoReconnectOptions: {
61+
attempts: e.autoReconnectOptions && e.autoReconnectOptions.attempts || 0,
62+
minInterval: e.autoReconnectOptions && e.autoReconnectOptions.maxInterval || 500,
63+
maxInterval: e.autoReconnectOptions && e.autoReconnectOptions.maxInterval || 2e3
64+
},
65+
logger: e.loggerOptions && e.loggerOptions.logger ? e.loggerOptions.logger : new Logger(e.loggerOptions && e.loggerOptions.level || LogLevel.ALL)
6266
}, !this.options.url) return this.options.logger.error("url must be provided");
6367
this.emitter = new EventEmitter(this.options.logger), this.options.autoConnect && this.connect();
6468
}
65-
return Object.defineProperty(e.prototype, "readyState", {
69+
return Object.defineProperty(e.prototype, "OPEN", {
70+
get: function() {
71+
return this.socket.OPEN;
72+
},
73+
enumerable: !0,
74+
configurable: !0
75+
}), Object.defineProperty(e.prototype, "CLOSED", {
76+
get: function() {
77+
return this.socket.CLOSED;
78+
},
79+
enumerable: !0,
80+
configurable: !0
81+
}), Object.defineProperty(e.prototype, "readyState", {
6682
get: function() {
6783
return this.socket ? this.socket.readyState : 0;
6884
},
@@ -80,10 +96,17 @@ var EventEmitter = function() {
8096
}), e.prototype.connect = function() {
8197
var e = this;
8298
if (this.isCreated) return this.options.logger.error("Connect event has been called multiple times");
83-
this.isCreated = !0, this.socket = new Socket(this.options.url), this.socket.binaryType = "arraybuffer",
84-
this.socket.onopen = function() {}, this.socket.onclose = function() {}, this.socket.onmessage = function(t) {
99+
this.isCreated = !0, this.socket = new Socket(this.options.url), this.socket.onopen = function() {},
100+
this.socket.onclose = function(t, o) {
101+
e.isCreated = !1;
102+
var n = "number" == typeof t ? t : t.code, r = "number" == typeof t ? o : t.reason;
103+
if (e.emitter.emit("close", n, r), e.options.autoReconnect && 1e3 !== n && e.readyState === e.CLOSED) return setTimeout(function() {
104+
e.connect();
105+
}, Math.floor(Math.random() * (e.options.autoReconnectOptions.maxInterval - e.options.autoReconnectOptions.minInterval + 1)));
106+
e.emitter.removeEvents();
107+
}, this.socket.onmessage = function(t) {
85108
var o = t;
86-
t.data && (o = t.data), e.withPing(o, function() {
109+
t.data && (o = t.data), e.parsePing(o, function() {
87110
if (e.emitter.exist("message")) return e.emitter.emit("message", o);
88111
e.processMessage(o);
89112
});
@@ -95,7 +118,7 @@ var EventEmitter = function() {
95118
this.emitter.on(e, t);
96119
}, e.prototype.close = function(e, t) {
97120
this.socket.close(e || 1e3, t);
98-
}, e.prototype.processMessage = function(e) {}, e.prototype.withPing = function(e, t) {
121+
}, e.prototype.processMessage = function(e) {}, e.prototype.parsePing = function(e, t) {
99122
var o = this;
100123
if (1 === e.size || 1 === e.byteLength) {
101124
var n = function(e) {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /