implement StatusProvider.send for Android and iOS (only http)

This commit is contained in:
Roman Volosovskyi 2017-02-01 10:51:20 +02:00 committed by Roman Volosovskyi
parent 0d9f35190f
commit 80f4c54357
1 changed files with 53 additions and 11 deletions

View File

@ -5,17 +5,16 @@ function httpCallback(id, data) {
var result = data;
var error = null;
console.log(data);
try {
result = JSON.parse(data);
} catch (e) {
error = {message: "InvalidResponse"};
}
if (callbacks[id]) {
callbacks[id](error, result);
}
}
var StatusHttpProvider = function (host, timeout) {
this.host = host || 'http://localhost:8545';
@ -23,10 +22,50 @@ var StatusHttpProvider = function (host, timeout) {
};
StatusHttpProvider.prototype.send = function (payload) {
if (typeof StatusBridge == "undefined") {
if (window.location.protocol == "https:") {
throw new Error('You tried to send "' + payload.method + '" synchronously. Synchronous requests are not supported, sorry.');
}
var request = this.prepareRequest(false);
try {
request.send(JSON.stringify(payload));
} catch (error) {
throw errors.InvalidConnection(this.host);
}
var result = request.responseText;
try {
result = JSON.parse(result);
} catch (e) {
throw errors.InvalidResponse(request.responseText);
}
return result;
} else {
result = StatusBridge.sendRequestSync(JSON.stringify(payload));
try {
result = JSON.parse(result);
} catch (e) {
throw new Error("InvalidResponse: " + result);
}
return result;
}
};
StatusHttpProvider.prototype.sendAsync = function (payload, callback) {
StatusHttpProvider.prototype.prepareRequest = function () {
var request = new XMLHttpRequest();
request.open('POST', "http://localhost:8545", false);
request.setRequestHeader('Content-Type', 'application/json');
return request;
};
function sendAsync(payload, callback) {
var messageId = callbackId++;
callbacks[messageId] = callback;
@ -42,6 +81,8 @@ StatusHttpProvider.prototype.sendAsync = function (payload, callback) {
}
};
StatusHttpProvider.prototype.sendAsync = sendAsync;
/**
* Synchronously tries to make Http request
*
@ -55,7 +96,8 @@ StatusHttpProvider.prototype.isConnected = function() {
jsonrpc: '2.0',
method: 'net_listening',
params: []
}, function(){});
}, function () {
});
return true;
} catch (e) {
return false;