Websocket handling
This commit is contained in:
parent
3ae6bbf5a4
commit
a9065b1eae
|
@ -2,11 +2,63 @@ const httpProxy = require('http-proxy');
|
|||
const http = require('http');
|
||||
const constants = require('../constants.json');
|
||||
|
||||
exports.serve = function(ipc, host, port, ws){
|
||||
let commList = {};
|
||||
let transactions = {};
|
||||
let receipts = {};
|
||||
let commList = {};
|
||||
let transactions = {};
|
||||
let receipts = {};
|
||||
|
||||
const parseRequest = function(reqBody){
|
||||
let jsonO;
|
||||
try {
|
||||
jsonO = JSON.parse(reqBody);
|
||||
} catch(e){
|
||||
return; // Request is not a json. Do nothing
|
||||
}
|
||||
if(jsonO.method === "eth_sendTransaction"){
|
||||
commList[jsonO.id] = {
|
||||
type: 'contract-log',
|
||||
address: jsonO.params[0].to,
|
||||
data: jsonO.params[0].data
|
||||
};
|
||||
} else if(jsonO.method === "eth_getTransactionReceipt"){
|
||||
if(transactions[jsonO.params[0]]){
|
||||
transactions[jsonO.params[0]].receiptId = jsonO.id;
|
||||
receipts[jsonO.id] = transactions[jsonO.params[0]].commListId;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const parseResponse = function(ipc, resBody){
|
||||
let jsonO;
|
||||
try {
|
||||
jsonO = JSON.parse(resBody);
|
||||
} catch(e) {
|
||||
return; // Response is not a json. Do nothing
|
||||
}
|
||||
|
||||
if(commList[jsonO.id]){
|
||||
commList[jsonO.id].transactionHash = jsonO.result;
|
||||
transactions[jsonO.result] = {commListId: jsonO.id};
|
||||
} else if(receipts[jsonO.id] && jsonO.result && jsonO.result.blockNumber){
|
||||
commList[receipts[jsonO.id]].blockNumber = jsonO.result.blockNumber;
|
||||
commList[receipts[jsonO.id]].gasUsed = jsonO.result.gasUsed;
|
||||
commList[receipts[jsonO.id]].status = jsonO.result.status;
|
||||
|
||||
if(ipc.connected && !ipc.connecting){
|
||||
ipc.request('log', commList[receipts[jsonO.id]]);
|
||||
} else {
|
||||
ipc.connecting = true;
|
||||
ipc.connect(() => {
|
||||
ipc.connecting = false;
|
||||
});
|
||||
}
|
||||
|
||||
delete transactions[commList[receipts[jsonO.id]].transactionHash];
|
||||
delete receipts[jsonO.id];
|
||||
delete commList[jsonO.id];
|
||||
}
|
||||
}
|
||||
|
||||
exports.serve = function(ipc, host, port, ws){
|
||||
let proxy = httpProxy.createProxyServer({
|
||||
target: {
|
||||
host,
|
||||
|
@ -25,34 +77,8 @@ exports.serve = function(ipc, host, port, ws){
|
|||
proxyRes.on('data', (b) => resBody.push(b));
|
||||
proxyRes.on('end', function () {
|
||||
resBody = Buffer.concat(resBody).toString();
|
||||
|
||||
let jsonO;
|
||||
try {
|
||||
jsonO = JSON.parse(resBody);
|
||||
} catch(e) {
|
||||
return;
|
||||
}
|
||||
|
||||
if(commList[jsonO.id]){
|
||||
commList[jsonO.id].transactionHash = jsonO.result;
|
||||
transactions[jsonO.result] = {commListId: jsonO.id};
|
||||
} else if(receipts[jsonO.id]){
|
||||
commList[receipts[jsonO.id]].blockNumber = jsonO.result.blockNumber;
|
||||
commList[receipts[jsonO.id]].gasUsed = jsonO.result.gasUsed;
|
||||
commList[receipts[jsonO.id]].status = jsonO.result.status;
|
||||
|
||||
if(ipc.connected && !ipc.connecting){
|
||||
ipc.request('log', commList[receipts[jsonO.id]]);
|
||||
} else {
|
||||
ipc.connecting = true;
|
||||
ipc.connect(() => {
|
||||
ipc.connecting = false;
|
||||
});
|
||||
}
|
||||
|
||||
delete transactions[commList[receipts[jsonO.id]].transactionHash];
|
||||
delete receipts[jsonO.id];
|
||||
delete commList[jsonO.id];
|
||||
if(resBody){
|
||||
parseResponse(ipc, resBody);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
@ -63,19 +89,7 @@ exports.serve = function(ipc, host, port, ws){
|
|||
.on('end', () => {
|
||||
reqBody = Buffer.concat(reqBody).toString();
|
||||
if(reqBody){
|
||||
let jsonO = JSON.parse(reqBody);
|
||||
if(jsonO.method === "eth_sendTransaction"){
|
||||
commList[jsonO.id] = {
|
||||
type: 'contract-log',
|
||||
address: jsonO.params[0].to,
|
||||
data: jsonO.params[0].data
|
||||
};
|
||||
} else if(jsonO.method === "eth_getTransactionReceipt"){
|
||||
if(transactions[jsonO.params[0]]){
|
||||
transactions[jsonO.params[0]].receiptId = jsonO.id;
|
||||
receipts[jsonO.id] = transactions[jsonO.params[0]].commListId;
|
||||
}
|
||||
}
|
||||
parseRequest(reqBody);
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -85,9 +99,26 @@ exports.serve = function(ipc, host, port, ws){
|
|||
});
|
||||
|
||||
if(ws){
|
||||
const WsParser = require('simples/lib/parsers/ws'); // npm install simples
|
||||
|
||||
server.on('upgrade', function (req, socket, head) {
|
||||
proxy.ws(req, socket, head);
|
||||
});
|
||||
|
||||
proxy.on('open', (proxySocket) => {
|
||||
proxySocket.on('data', (data) => {
|
||||
parseResponse(ipc, data.toString().substr(data.indexOf("{")));
|
||||
});
|
||||
});
|
||||
|
||||
proxy.on('proxyReqWs', (proxyReq, req, socket) => {
|
||||
var parser = new WsParser(0, false);
|
||||
socket.pipe(parser);
|
||||
parser.on('frame', function (frame) {
|
||||
parseRequest(frame.data);
|
||||
});
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
server.listen(port);
|
||||
|
|
|
@ -8,11 +8,11 @@ class ConsoleListener {
|
|||
this.addressToContract = [];
|
||||
this.contractsConfig = embark.config.contractsConfig;
|
||||
this.contractsDeployed = false;
|
||||
|
||||
this._listenForLogRequests();
|
||||
|
||||
this.events.on("contractsDeployed", () => {
|
||||
this.contractsDeployed = true;
|
||||
this._updateContractList();
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -23,6 +23,8 @@ class ConsoleListener {
|
|||
return;
|
||||
}
|
||||
contractsList.forEach(contract => {
|
||||
if(!contract.deployedAddress) return;
|
||||
|
||||
let address = contract.deployedAddress.toLowerCase();
|
||||
if(!this.addressToContract[address]){
|
||||
let funcSignatures = {};
|
||||
|
@ -49,7 +51,7 @@ class ConsoleListener {
|
|||
});
|
||||
}
|
||||
|
||||
_listenForLogRequests(){
|
||||
_listenForLogRequests(){
|
||||
if(this.ipc.ipcRole !== 'server') return;
|
||||
this.ipc.on('log', (request) => {
|
||||
if(request.type == 'contract-log'){
|
||||
|
@ -59,9 +61,9 @@ class ConsoleListener {
|
|||
if(!this.addressToContract[address]){
|
||||
this._updateContractList();
|
||||
}
|
||||
|
||||
if(!this.addressToContract[address]) return;
|
||||
|
||||
|
||||
const name = this.addressToContract[address].name;
|
||||
const func = this.addressToContract[address].functions[data.substring(0, 10)];
|
||||
const functionName = func.functionName;
|
||||
|
|
|
@ -65,6 +65,7 @@
|
|||
"request": "^2.85.0",
|
||||
"serve-static": "^1.11.1",
|
||||
"shelljs": "^0.5.0",
|
||||
"simples": "^0.8.8",
|
||||
"solc": "0.4.24",
|
||||
"string-replace-async": "^1.2.1",
|
||||
"style-loader": "^0.19.0",
|
||||
|
|
Loading…
Reference in New Issue