add StatusHttpProview for accessing web3 in webview

This commit is contained in:
Roman Volosovskyi 2016-12-29 20:28:30 +02:00
parent 3258df51c8
commit 323a91760f
2 changed files with 66 additions and 2 deletions

View File

@ -68,7 +68,7 @@
"react-native-tcp": "^2.0.4", "react-native-tcp": "^2.0.4",
"react-native-udp": "^1.2.6", "react-native-udp": "^1.2.6",
"react-native-vector-icons": "^2.0.3", "react-native-vector-icons": "^2.0.3",
"react-native-webview-bridge": "github:status-im/react-native-webview-bridge#0.33.3", "react-native-webview-bridge": "github:status-im/react-native-webview-bridge#0.33.4",
"readable-stream": "^1.0.33", "readable-stream": "^1.0.33",
"realm": "^0.14.3", "realm": "^0.14.3",
"stream-browserify": "^1.0.0", "stream-browserify": "^1.0.0",

View File

@ -1 +1,65 @@
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545")); var callbackId = 0;
var callbacks = {};
function httpCallback(id, data) {
var result = data;
var error = null;
console.log(data);
try {
result = JSON.parse(data);
} catch(e) {
error = {message: "InvalidResponse"};
}
callbacks[id](error, result);
}
var StatusHttpProvider = function (host, timeout) {
this.host = host || 'http://localhost:8545';
this.timeout = timeout || 0;
};
StatusHttpProvider.prototype.send = function (payload) {
throw new Error('You tried to send "'+ payload.method +'" synchronously. Synchronous requests are not supported, sorry.');
};
StatusHttpProvider.prototype.sendAsync = function (payload, callback) {
var messageId = callbackId++;
callbacks[messageId] = callback;
if(typeof StatusBridge == "undefined") {
var data = {
payload: JSON.stringify(payload),
callbackId: JSON.stringify(messageId)
};
webkit.messageHandlers.sendRequest.postMessage(JSON.stringify(data));
} else {
StatusBridge.sendRequest(JSON.stringify(messageId), JSON.stringify(payload));
}
};
/**
* Synchronously tries to make Http request
*
* @method isConnected
* @return {Boolean} returns true if request haven't failed. Otherwise false
*/
StatusHttpProvider.prototype.isConnected = function() {
try {
this.sendAsync({
id: 9999999999,
jsonrpc: '2.0',
method: 'net_listening',
params: []
}, function(){});
return true;
} catch(e) {
return false;
}
};
web3 = new Web3(new StatusHttpProvider("http://localhost:8545"));