devebot-api
Advanced tools
Comparing version 0.1.4 to 0.1.5
30
index.js
@@ -22,4 +22,3 @@ 'use strict'; | ||
self.execCommand = function(command, callback) { | ||
var wsUrl = util.format('ws://%s:%s%s/execute', config.host, config.port, config.path); | ||
var ws = new WebSocket(wsUrl); | ||
var ws = buildWebsocketClient(config); | ||
@@ -77,2 +76,3 @@ var wsCommand = { | ||
ws.on('close', function handler(code, message) { | ||
self.emit('close', code, message); | ||
logger.debug(' - Websocket@client is closed, code: %s, message: %s', code, message); | ||
@@ -82,2 +82,3 @@ }); | ||
ws.on('error', function handler(error) { | ||
self.emit('error', error); | ||
logger.debug(' - Websocket@client encounter an error: %s', error); | ||
@@ -92,2 +93,27 @@ }); | ||
var buildWebsocketClient = function(config) { | ||
var wsOpts = {}; | ||
if (config.authen instanceof Object) { | ||
wsOpts.headers = wsOpts.headers || {}; | ||
if (config.authen.token_jwt) { | ||
wsOpts.headers["Authorization"] = "JWT " + config.authen.token_jwt; | ||
} | ||
if (config.authen.token_key && config.authen.token_secret) { | ||
wsOpts.headers["X-Token-Key"] = config.authen.token_key; | ||
wsOpts.headers["X-Token-Secret"] = config.authen.token_secret; | ||
} | ||
} | ||
var sslEnabled = (config.tunnel instanceof Object && config.tunnel.enabled); | ||
if (sslEnabled) { | ||
wsOpts.rejectUnauthorized = config.tunnel.rejectUnauthorized || false; | ||
} | ||
var wsUrl = util.format('%s://%s:%s%s/execute', | ||
sslEnabled?'wss':'ws', config.host, config.port, config.path); | ||
return new WebSocket(wsUrl, null, wsOpts); | ||
}; | ||
var emptyFunction = function() {}; | ||
@@ -94,0 +120,0 @@ |
{ | ||
"name": "devebot-api", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"description": "Devebot API", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
5855
99