grexxconnect-ess
Advanced tools
Comparing version 1.0.3 to 1.0.5
76
index.js
@@ -8,3 +8,3 @@ /*! Copyright (C) Grexx - All Rights Reserved | ||
const EventEmitter = require('events'); | ||
const WebSocket = require('ws'); | ||
const WebSocket = require('./client'); | ||
@@ -17,26 +17,30 @@ class EssConnect extends EventEmitter { | ||
const ws = new WebSocket(`${settings.server}/`, { | ||
headers: { | ||
headers: { | ||
'User-Agent': `ESS-Node-Agent v${thisVersion} - ${settings.applicationName}`, | ||
'Authorization': "Basic " + new Buffer(settings.username + ":" + settings.password, "utf8").toString("base64") | ||
'Authorization': "Basic " + new Buffer(`${settings.username}:${settings.password}`, "utf8").toString("base64") | ||
}, | ||
//rejectUnauthorized: true, | ||
//checkServerIdentity: true, | ||
debug: settings.debug | ||
}); | ||
const send = ( type, content ) => { | ||
ws.send(JSON.stringify({ | ||
type: type, | ||
content: content, | ||
timestamp: Date.now() | ||
})); | ||
return new Promise(( resolve, reject ) => { | ||
ws.send(JSON.stringify({ | ||
type: type, | ||
content: content, | ||
timestamp: Date.now() | ||
}), error => { | ||
if ( error ) { | ||
reject(error); | ||
} else { | ||
resolve(); | ||
} | ||
}); | ||
}); | ||
}; | ||
ws.on('open', () => this.emit('connected')); | ||
ws.on('close', () => this.emit('disconnected')); | ||
ws.on('message', data => { | ||
//console.log(`[Incoming]`, data); | ||
const _message = JSON.parse(data); | ||
ws.on('close', ( code, msg ) => this.emit('disconnected', code, msg)); | ||
ws.on('message', _message => { | ||
switch ( _message.type ) { | ||
case 'ping': | ||
this.ws.debug('responding to connection-ping'); | ||
send('pong'); | ||
@@ -46,4 +50,8 @@ this.emit('ping'); | ||
case 'authorized': | ||
this.log('connected and authorized for'.green, _message.namespace.blue); | ||
this.emit('authorized', _message.namespace); | ||
break; | ||
case 'system': | ||
this.handleSystemMessage(_message, send); | ||
break; | ||
case'message': | ||
@@ -54,4 +62,3 @@ this.emit('message', { | ||
resolve: outputData => { | ||
//console.info('Resolving Message with', outputData); | ||
send('response', { | ||
return send('response', { | ||
toMessage: _message, | ||
@@ -64,3 +71,3 @@ outputData: outputData | ||
default: | ||
console.warn(`Unknown MessageType ${_message.type}`); | ||
this.log(`Unknown MessageType ${_message.type}`.yellow); | ||
break; | ||
@@ -72,3 +79,3 @@ } | ||
ws.on('error', error => { | ||
console.error('ESS-Error', error); | ||
//this.log('ESS-Error'.red, error.toString().red); | ||
this.emit('error', error); | ||
@@ -78,7 +85,32 @@ }); | ||
ws.on('timeout', error => { | ||
console.error('ESS-Timeout', error); | ||
this.log('ESS-Timeout'.red, error.toString().red); | ||
this.emit('error', 'TimeOut'); | ||
}) | ||
}); | ||
this.ws = ws; | ||
} | ||
/**Set a monitoring function. Should return {status:'ok|warning|error', ...} | ||
* @param {function} statusFunction | ||
*/ | ||
setMonitoringResponse( statusFunction ) { | ||
this._statusFunction = statusFunction; | ||
} | ||
log( ...args ) { | ||
console.info(' [ESS][MAIN] '.gray, ...args); | ||
} | ||
handleSystemMessage( message, send ) { | ||
if ( message.content.action === 'ping' ) { | ||
this.ws.debug('responding to system-ping'); | ||
send('system', { | ||
action: 'pong', | ||
client_data: typeof this._statusFunction === 'function' ? | ||
this._statusFunction() : { status: 'ok' }, | ||
server_data: message.content.id | ||
}); | ||
} | ||
} | ||
parseMessage( message = [] ) { | ||
@@ -85,0 +117,0 @@ const result = {}; |
@@ -5,3 +5,3 @@ { | ||
"description" : "Grexx Connect - External System Service Helper", | ||
"version" : "1.0.3", | ||
"version" : "1.0.5", | ||
"contributors" : [ | ||
@@ -14,3 +14,4 @@ { | ||
"dependencies" : { | ||
"ws": "3.3" | ||
"colors": "1.2", | ||
"ws" : "3.3" | ||
}, | ||
@@ -25,2 +26,2 @@ "optionalDependencies": { | ||
} | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9743
4
216
4
+ Addedcolors@1.2
+ Addedcolors@1.2.5(transitive)