A minimalist RPC designed for alt:V
alt:V rpc need to be included in every resource in which you want to use RPC system!
- Download rpc files
- Put "src" the folder in your ressource
- Include folder in resource.cfg file
- Finally import RPC inside the client
import rpc from 'yourpath/rpc.mjs';
- Download rpc files
- Put "src" the folder in your ressource
- Import RPC inside your serve file
import rpc from 'yourpath/rpc.mjs';
- Just have to link javascript file "rpc.browser.mjs"
<html>
<head>
<title>My Page</title>
<script type="text/javascript" src="./rpc.browser.mjs"></script>
<script type="text/javascript">
rpc.register('peace', () => 'hello from browser!');
// ...
</script>
</head>
</html>
ps: browser rpc system requires to instantiate view via createView(name, url) to use rpc functions.
Used to register an procedure in the current context that will be call after.
name
string - The unique identifier, relative to the current context, of the procedure.callback
function - The procedure. This function will receive 1 arguments.arg
The arguments that were provided by the caller.
rpc.register('sayhello', () => 'hello')
Returns 'hello' to the caller asynchronously.
rpc.register('getUser', async (id) => {
const user = await getUserFromId(id);
return user;
});
Returns the resolved user to the caller.
unregister an procedure in the current context.
name
string - The unique identifier, relative to the current context, of the procedure.
rpc.unregister('sayhello')
call a procedure in the current context.
name
string - The unique identifier, relative to the current context, of the procedure.callback
function - The procedure. This function will receive 1 arguments.arg
The arguments that were provided by the caller.
rpc.register('sayhello', () => 'hello');
rpc.call('sayhello').then((result) => {
console.log(result)
// result return hello
})
call a procedure register in client context.
player
Player - the instance of the player who is targeted.name
string - The unique identifier, relative to the current context, of the procedure.args
Optional arguments to pass to the procedure.
client
rpc.register('sayhello', () => 'hello');
server
rpc.callClient(player,'sayhello').then((result) => {
console.log(result)
// result return hello
}).catch(err => {
// if name doesn't exist in client context
// handle error
});
call a procedure register in browser context.
player
Player - the instance of the player who is targeted.viewname
string - The unique identifier, relative to view declared before.name
string - The unique identifier, relative to the current context, of the procedure.args
Optional arguments to pass to the procedure.
browser
rpc.register('myevent', () => '42');
server
rpc.callBrowser(player, 'myview', 'myevent').then((result) => {
console.log(result)
// result return 42
}).catch(err => {
// if name doesn't exist in client context
// handle error
});
call a procedure register in server context.
name
string - The unique identifier, relative to the current context, of the procedure.args
Optional arguments to pass to the procedure.
server
rpc.register('myevent', () => '42');
client
//in server file
rpc.callServer('myevent').then((result) => {
alt.log(result)
// result return 42
}).catch(err => {
// if name doesn't exist in server context
// handle error
});
call a procedure register in browser context.
viewname
string - The unique identifier, relative to view declared before.name
string - The unique identifier, relative to the current context, of the procedure.args
Optional arguments to pass to the procedure.
browser
rpc.register('myevent', () => '42');
client
rpc.callBrowser(player, 'view1', 'myevent').then((result) => {
alt.log(result)
// result return 42
}).catch(err => {
// if name doesn't exist in client context
// handle error
});
create an view instance. required to use rpc function in browser files
name
string - The unique identifier of selected view.url
string - the url to have acces to your view.
let view = rpc.createView('myView', 'http://resource/myUrl');
return a view instance
rpc.detroyView('myView');
destroy view
let view = rpc.getView('myView');
return view instance
let viewsArray = rpc.getViews();
return all view instance in an array
call a procedure register in client context.
name
string - The unique identifier, relative to the current context, of the procedure.args
Optional arguments to pass to the procedure.
client
rpc.register('myevent', () => '42');
browser
rpc.callClient('myevent').then((result) => {
console.log(result)
// result return 42
}).catch(err => {
// if name doesn't exist in client context
// handle error
});
call a procedure register in server context.
name
string - The unique identifier, relative to the current context, of the procedure.args
Optional arguments to pass to the procedure.
server
rpc.register('myevent', () => '42');
browser
//in server file
rpc.callServer('myevent').then((result) => {
console.log(result)
// result return 42
}).catch(err => {
// if name doesn't exist in server context
// handle error
});
23/10/2019 : add second argument to register in server that contain player and procedure name
rpc.register('myprocedure', (args, info) => {
console.log(info);
// info return { player: Player {}, name: 'myprocedure'}
});