Skip to content

Commit a9bc812

Browse files
authored
Merge pull request #105 from Pradhyumna02/WebSockets-Bug-Fix
Added try catch while creating the websocket connection
2 parents 2b7c05f + e5de203 commit a9bc812

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "speed-testjs",
3-
"version": "1.0.28",
3+
"version": "1.0.29",
44
"description": "measure internet bandwidth",
55
"main": "index.js",
66
"author": "Maulan Byron",

public/lib/webSocket.js

+16-8
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,17 @@
3838
* Initiate the request
3939
*/
4040
webSocket.prototype.start = function () {
41-
if (this._request === null ||
42-
typeof this._request === 'undefined') {
43-
this._request = new WebSocket(this.url);
44-
this._request.onopen = this._handleOnOpen.bind(this);
45-
this._request.onmessage = this._handleOnMessage.bind(this);
46-
this._request.onclose = this._handleOnClose.bind(this);
47-
this._request.onerror = this._handleOnError.bind(this);
41+
if (this._request === null || typeof this._request === 'undefined') {
42+
try {
43+
this._request = new WebSocket(this.url);
44+
this._request.onopen = this._handleOnOpen.bind(this);
45+
this._request.onmessage = this._handleOnMessage.bind(this);
46+
this._request.onclose = this._handleOnClose.bind(this);
47+
this._request.onerror = this._handleOnError.bind(this);
48+
} catch (err) {
49+
this.callbackOnError('connection error');
50+
}
51+
4852
}
4953
};
5054

@@ -96,7 +100,11 @@
96100
* close webSocket
97101
*/
98102
webSocket.prototype.close = function () {
99-
this._request.close();
103+
try {
104+
this._request.close();
105+
} catch (error) { // jshint ignore:line
106+
107+
}
100108
};
101109

102110

0 commit comments

Comments
 (0)