initial test using websockets

This commit is contained in:
Martin Schröder 2016-01-15 13:30:20 +01:00
parent 1565ff36c1
commit 8cb80181ec
4 changed files with 61 additions and 6 deletions

View File

@ -7,7 +7,8 @@ var fs = require("fs");
var request = require("request");
var http = require("http");
var bodyParser = require('body-parser')
var config = { port: 3000, ubus_uri: "http://192.168.1.1/ubus" };
var config = { port: 3000 };
var exec = require("child_process").exec;
//check if file exists and is a file
if (fs.existsSync("juci-local-server.config")){
@ -271,7 +272,22 @@ app.post('/ubus', function(req, res) {
timedOut = true;
sendResponse(body);*/
}, 5000);
var cmd = "";
data.params[3].ubus_rpc_session = data.params[0];
if(data.method == "call") cmd = "sudo ubus2 -s /tmp/router-ubus-json.sock "+data.method+" "+data.params[1]+" "+data.params[2]+" '"+JSON.stringify(data.params[3])+"'";
else if(data.method == "list") cmd = "sudo ubus2 -s /tmp/router-ubus-json.sock list";
console.log("command: "+cmd);
exec(cmd, function(error, stdout, stderr){
console.log("Got stdout: "+stdout+", stderr: "+stderr);
var resp = JSON.parse(stdout);
sendResponse({
jsonrpc:"2.0",
id: 1,
result: [0, resp[0]]
});
});
/*
request({
url: config.ubus_uri,
method: "POST",
@ -301,6 +317,7 @@ app.post('/ubus', function(req, res) {
sendResponse(body);
}
});
*/
//console.log("Unknown RPC call "+name);
//res.end();
}

View File

@ -28,7 +28,9 @@
"local.features",
"local.set_rpc_host"
];
var connect = null;
function rpc_request(type, namespace, method, data){
var sid = "";
@ -51,9 +53,31 @@
if(RPC_CACHE[k].deferred && RPC_CACHE[k].deferred.state() == "pending"){
retain[k] = RPC_CACHE[k];
}
});
});
RPC_CACHE = retain;
data.ubus_rpc_session = RPC_SESSION_ID;
if(!connect) connect = $rpc2.$connect("ws://localhost:1234");
connect.done(function(){
if(type == "call"){
$rpc2.$call(namespace, method, data).done(function(ret){
var def = RPC_CACHE[key].deferred;
if(ret[0] && (ret[0].code != undefined || ret[0].error)){
console.log("FAIL request: "+type+", object="+namespace+", method="+method+", data="+JSON.stringify(data)+", resp="+JSON.stringify(ret));
def.reject(ret[0] || {});
}
else {
console.log("request: "+type+", object="+namespace+", method="+method+", data="+JSON.stringify(data)+", resp="+JSON.stringify(ret));
def.resolve(ret[0]);
}
});
} else if(type == "list"){
$rpc2.$list().done(function(ret){
RPC_CACHE[key].deferred.resolve(ret[0] || {});
});
}
});
/*
// setup default rpcs
$.jsonRPC.withOptions({
namespace: "",
@ -108,6 +132,7 @@
}
})
});
*/
return RPC_CACHE[key].deferred.promise();
}

View File

@ -98,6 +98,19 @@
}
RPC.prototype.$list = function(){
var self = this;
self.seq++;
var req = self.requests[self.seq] = {
id: self.seq,
deferred: $.Deferred()
};
self.socket.send(JSON.stringify({
jsonrpc: "2.0",
id: req.id,
method: "call",
params: ["/ubus/peer", "ubus.peer.list", {}]
})+"\n");
return req.deferred.promise();
}

View File

@ -20,7 +20,7 @@ $rpc2.$connect(params.host).done(function(){
var t = Date.now();
if(params.method == "list"){
$rpc2.$call("/ubus/server", "ubus.server.list", {}).done(function(ret){
$rpc2.$call("/ubus/peer", "ubus.peer.list", {}).done(function(ret){
var n = Date.now();
var objects = ret[0];
Object.keys(objects).map(function(k){