xapi-connector
Version:
Simple low level connector for xAPI written in Coffeescript
89 lines (68 loc) • 2.2 kB
JavaScript
// Generated by CoffeeScript 1.8.0
(function() {
var CONN_PORT, Connector, PASSWORD, SERVER_URL, STREAM_PORT, USERNAME, client, print;
Connector = require('../lib/xapi-connector.js');
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
SERVER_URL = 'xapia.x-station.eu';
CONN_PORT = '5144';
STREAM_PORT = '5145';
USERNAME = '201870';
PASSWORD = 'rz3smI';
print = function(msg) {
return console.log(msg);
};
client = new Connector(SERVER_URL, CONN_PORT, STREAM_PORT, USERNAME, PASSWORD);
client.on('open', function() {
var msg;
print('Successfuly connected, login in');
msg = client.buildCommand('login', {
userId: client.username,
password: client.password
}, 'login');
return client.send(msg);
});
client.on('message', function(msg) {
print("Received a message: " + msg);
msg = JSON.parse(msg);
if (msg.customTag === 'login') {
if (msg.status === true) {
print('Successfuly loged in, connecting to stream');
client.stream_session_id = msg.streamSessionId;
return client.connectStream();
} else {
return print('Login failed');
}
} else if (msg.customTag === 'logout') {
client.disconnectStream();
return client.disconnect();
}
});
client.on('error', function(err) {
return print("Connection error: " + err);
});
client.on('close', function() {
return print('Connection closed');
});
client.onStream('open', function() {
var msg;
print('Successfuly connected to stream, subscribing to indicators');
msg = client.buildStreamCommand('getAccountIndicators', client.stream_session_id);
return client.sendStream(msg);
});
client.onStream('message', function(msg) {
return print("Received a message from the stream: " + msg);
});
client.onStream('error', function(err) {
return print("Stream error: " + err);
});
client.onStream('close', function() {
return print('Stream closed');
});
client.connect();
setTimeout(function() {
var msg;
print('login out');
msg = client.buildCommand('logout', null, 'logout');
return client.send(msg);
}, 10000);
}).call(this);