Add CEF-->server RFC

This commit is contained in:
Micah Allen 2018-11-01 16:40:00 -04:00
parent 2794c0e33b
commit 71eaaf4da8

View File

@ -16,9 +16,20 @@ async function callProcedure(name, args, info){
} }
const processEvent = (...args) => { const processEvent = (...args) => {
let data = args[0]; let rawData = args[0];
if(environment === "server") data = args[1]; if(environment === "server") rawData = args[1];
data = util.parseData(data); const data = util.parseData(rawData);
if(data.thru && environment === "client"){
if(data.req){ // a CEF request is trying to get to the server
mp.events.callRemote(PROCESS_EVENT, rawData);
}else if(data.ret){ // a server response is trying to get to a CEF instance
mp.browsers.forEach(browser => {
browser.execute(`var process = window["${PROCESS_EVENT}"] || function(){}; process('${rawData}');`); // send data to every instance
});
}
return;
}
if(data.req){ // someone is trying to remotely call a procedure if(data.req){ // someone is trying to remotely call a procedure
const info = { const info = {
@ -29,16 +40,19 @@ const processEvent = (...args) => {
const promise = callProcedure(data.name, data.args, info); const promise = callProcedure(data.name, data.args, info);
switch(environment){ switch(environment){
case "server": { case "server": {
const part = {
ret: 1,
id: data.id
};
if(data.thru) part.thru = 1;
promise.then(res => { promise.then(res => {
info.player.call(PROCESS_EVENT, [util.stringifyData({ info.player.call(PROCESS_EVENT, [util.stringifyData({
ret: 1, ...part,
id: data.id,
res res
})]); })]);
}).catch(err => { }).catch(err => {
info.player.call(PROCESS_EVENT, [util.stringifyData({ info.player.call(PROCESS_EVENT, [util.stringifyData({
ret: 1, ...part,
id: data.id,
err err
})]); })]);
}); });
@ -71,7 +85,11 @@ const processEvent = (...args) => {
} }
}; };
if(environment === "cef"){
window[PROCESS_EVENT] = processEvent;
}else{
mp.events.add(PROCESS_EVENT, processEvent); mp.events.add(PROCESS_EVENT, processEvent);
}
/** /**
* Register a procedure. * Register a procedure.
@ -125,6 +143,23 @@ rpc.callServer = (name, args) => {
})); }));
}); });
} }
case "cef": {
const id = util.uid();
return new Promise((resolve, reject) => {
pending[id] = {
resolve,
reject
};
mp.trigger(PROCESS_EVENT, util.stringifyData({
req: 1,
id,
name,
env: environment,
args,
thru: 1
}));
});
}
} }
}; };