-
Notifications
You must be signed in to change notification settings - Fork 0
/
json-socket-connection.js
143 lines (125 loc) · 3.94 KB
/
json-socket-connection.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
// A client websocket connect which attempts to reconnect after disconnection and parse JSON message
// #Modules
var EventEmitter = require( 'events' ).EventEmitter;
var util = require( 'util' );
// #NPM
var WS = require( 'ws' );
// #JSONSocketConnection
// Options
// * socketUrl <String>
// * attemptReconnectionAfterMS <Number> - wait until attempting reconnection. Default 1000.
// Events
// * 'open'
// * 'closed'
// * 'reconnecting'
// * 'closed successfully'
// * 'json'
// * 'nonjson'
// * 'message'
function JSONSocketConnection ( options ) {
this.socketUrl = options.socketUrl;
this._connectionDesired = false;
this.attemptReconnectionAfterMS = (typeof options.attemptReconnectionAfterMS !== 'undefined') ? options.attemptReconnectionAfterMS : 10000;
}
// Add event emitter
util.inherits(JSONSocketConnection, EventEmitter);
JSONSocketConnection.prototype.openSocketConnection = function () {
this._connectionDesired = true;
var self = this;
try {
self.socket = new WS( self.socketUrl );
} catch (err) {
console.log( 'Failed to create WS' );
return self.emit( 'error', err );
}
self.socket.on( 'open', function () {
self.emit( 'open' );
});
self.socket.on( 'close', function () {
shouldReconnect();
if ( !self._connectionDesired ) {
self.emit( 'closed successfully' );
}
});
self.socket.on( 'error', function ( err ) {
if ( err.code && err.code === 'ECONNREFUSED' ) {
console.log( 'JSONSocketConnnection connection refused', 'will close' );
shouldReconnect();
} else {
console.log( 'ws error', err );
}
});
self.socket.on( 'message', function ( message ) {
self.emit( 'message', message);
var wasError = false;
try {
message = JSON.parse( message );
} catch (e) {
self.emit('nonjson', message );
wasError = true;
return;
}
if ( !wasError ) {
self.emit('json', message);
}
});
function shouldReconnect () {
if ( self._connectionDesired ) {
self.socket.close();
// clean up
self.socket.onclose = undefined;
self.socket.onmessage = undefined;
self.socket.onerror = undefined;
delete self.socket;
// start again
setTimeout(function () {
console.log( 'Reconnecting' );
self.openSocketConnection();
}, self.attemptReconnectionAfterMS );
self.emit( 'reconnecting' );
}
}
};
JSONSocketConnection.prototype.closeSocketConnection = function () {
this._connectionDesired = false;
this.socket.close();
};
// @param obj <string/object> - if object it will be strignified
// @param callback <function> - called with (err)
JSONSocketConnection.prototype.send = function (obj, callback) {
if ( !this.socket ) { return; }
if (typeof obj === 'string') {
this.socket.send(obj, callback);
} else {
var str;
try {
str = JSON.stringify(obj);
} catch (e) {
if (callback) { callback(e); }
return;
}
this.socket.send(str, callback);
}
};
// ## Browser Style Event Shims
JSONSocketConnection.prototype.off = function () {
if ( arguments.length === 0 ) {
this.removeAllListeners();
} else if ( arguments.length === 1 ) {
this.removeAllListeners( arguments[1] );
} else {
this.removeListener.apply( this, arguments );
}
};
module.exports = JSONSocketConnection;
// # Utils
function getStrForReadyState ( readyState ) {
var state = "";
switch ( readyState ) {
case WS.CONNECTING: state = "Connecting"; break;
case WS.OPEN: state = "Open"; break;
case WS.CLOSING: state = "Closing"; break;
case WS.CLOSED: state = "Closed"; break;
}
return state;
}