Skip to content

Instantly share code, notes, and snippets.

@jvliwanag
Created September 12, 2012 05:31
Show Gist options
  • Save jvliwanag/3704476 to your computer and use it in GitHub Desktop.
Save jvliwanag/3704476 to your computer and use it in GitHub Desktop.
oacd_web websocket usage
<!DOCTYPE html>
<html>
<head>
<title>OpenACD API Test</title>
<script type="text/javascript" src="jsbn/jsbn.js"></script>
<script type="text/javascript" src="jsbn/prng4.js"></script>
<script type="text/javascript" src="jsbn/rng.js"></script>
<script type="text/javascript" src="jsbn/rsa.js"></script>
<script type="text/javascript">
var username = '1000';
var password = '1000';
var s = new WebSocket('ws://10.24.7.86:5555/wsock');
var cbks = {};
console.log('Created web socket');
console.dir(s);
s.onmessage = function(e) {
console.log('Received: ' + e.data);
var j = JSON.parse(e.data);
var requestId = j['request_id'];
if (cbks[requestId] !== undefined) {
cbks[requestId](j);
}
delete cbks[requestId];
}
var apiCtr = 1;
var callAPI = function(fun, args, cbk, id) {
if (args == null) args = [];
if (id == null) id = apiCtr++;
var request = {
request_id: id,
function: fun,
args: args
};
var str = JSON.stringify(request);
console.log('Sending: ' + str);
if (cbk !== null)
cbks[id] = cbk;
s.send(str);
}
s.onopen = function(e) {
console.log('connection opened!');
console.log('getting nonce');
callAPI('get_nonce', [], function(j) {
var salt = j.result.nonce;
var n = j.result.pubkey_n;
var e = j.result.pubkey_e;
var rsa = new RSAKey();
rsa.setPublic(n, e);
var decrypted = salt + password;
var encrypted = rsa.encrypt(decrypted);
callAPI('login', [username, encrypted], function(k) {
console.log('logged in?');
if (k.success) {
callAPI('set_release', [false]);
}
});
});
}
</script>
</head>
<body>
All the interesting stuff goes into the js console. ;)
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment