Add CEF-->client RPC

This commit is contained in:
Micah Allen 2018-11-01 16:54:32 -04:00
parent 71eaaf4da8
commit 5f26602327

View File

@ -15,6 +15,15 @@ async function callProcedure(name, args, info){
return listeners[name](args, info); return listeners[name](args, info);
} }
let passEventToBrowsers;
if(environment === "client"){
passEventToBrowsers = (raw) => {
mp.browsers.forEach(browser => {
browser.execute(`var process = window["${PROCESS_EVENT}"] || function(){}; process('${raw}');`);
});
};
}
const processEvent = (...args) => { const processEvent = (...args) => {
let rawData = args[0]; let rawData = args[0];
if(environment === "server") rawData = args[1]; if(environment === "server") rawData = args[1];
@ -24,9 +33,7 @@ const processEvent = (...args) => {
if(data.req){ // a CEF request is trying to get to the server if(data.req){ // a CEF request is trying to get to the server
mp.events.callRemote(PROCESS_EVENT, rawData); mp.events.callRemote(PROCESS_EVENT, rawData);
}else if(data.ret){ // a server response is trying to get to a CEF instance }else if(data.ret){ // a server response is trying to get to a CEF instance
mp.browsers.forEach(browser => { passEventToBrowsers(rawData);
browser.execute(`var process = window["${PROCESS_EVENT}"] || function(){}; process('${rawData}');`); // send data to every instance
});
} }
return; return;
} }
@ -59,19 +66,35 @@ const processEvent = (...args) => {
break; break;
} }
case "client": { case "client": {
const part = {
ret: 1,
id: data.id
};
if(data.env === "server"){
promise.then(res => { promise.then(res => {
mp.events.callRemote(PROCESS_EVENT, util.stringifyData({ mp.events.callRemote(PROCESS_EVENT, util.stringifyData({
ret: 1, ...part,
id: data.id,
res res
})); }));
}).catch(err => { }).catch(err => {
mp.events.callRemote(PROCESS_EVENT, util.stringifyData({ mp.events.callRemote(PROCESS_EVENT, util.stringifyData({
ret: 1, ...part,
id: data.id,
err err
})); }));
}); });
}else if(data.env === "cef"){
promise.then(res => {
passEventToBrowsers(util.stringifyData({
...part,
res
}));
}).catch(err => {
passEventToBrowsers(util.stringifyData({
...part,
err
}));
});
}
break; break;
} }
} }
@ -165,12 +188,17 @@ rpc.callServer = (name, args) => {
/** /**
* Calls a remote procedure registered on the client. * Calls a remote procedure registered on the client.
* @param player - The player to call the procedure on. * @param [player] - The player to call the procedure on.
* @param {string} name - The name of the registered procedure. * @param {string} name - The name of the registered procedure.
* @param args - Any parameters for the procedure. * @param args - Any parameters for the procedure.
* @returns {Promise} - The result from the procedure. * @returns {Promise} - The result from the procedure.
*/ */
rpc.callClient = (player, name, args) => { rpc.callClient = (player, name, args) => {
if(typeof player === "string"){
if(environment === "server") return Promise.reject('This syntax can only be used in browser and client environments.');
args = name;
name = player;
}
switch(environment){ switch(environment){
case "client": { case "client": {
if(player === mp.players.local) return rpc.call(name, args); if(player === mp.players.local) return rpc.call(name, args);
@ -192,6 +220,22 @@ rpc.callClient = (player, 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
}));
});
}
} }
}; };