Update README.md
This commit is contained in:
parent
dc0d914b27
commit
f7ee07af8b
100
README.md
100
README.md
@ -1,5 +1,4 @@
|
|||||||
**rage-eventbus** simplifies two-way communication between the RAGE Multiplayer server, client, and browser instances.
|
**rage-rpc** simplifies two-way communication between the RAGE Multiplayer server, client, and browser instances by providing a easy to use API for calling remote code and expecting results.
|
||||||
It can be used as a full-on replacement for RAGE's built-in `mp.events` API, providing consistency and clarity.
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@ -7,31 +6,31 @@ It can be used as a full-on replacement for RAGE's built-in `mp.events` API, pro
|
|||||||
|
|
||||||
### Server to Client
|
### Server to Client
|
||||||
|
|
||||||
**Situation:** The server wants to ask a specific player if they are currently running.
|
**Situation:** The server wants to ask a specific player if they are currently climbing anything.
|
||||||
|
|
||||||
|
##### Client-side
|
||||||
|
```javascript
|
||||||
|
const rpc = require('rage-rpc');
|
||||||
|
|
||||||
|
rpc.register('getIsClimbing', () => mp.players.local.isClimbing());
|
||||||
|
```
|
||||||
|
|
||||||
##### Server-side
|
##### Server-side
|
||||||
```javascript
|
```javascript
|
||||||
const rbus = require('rage-eventbus');
|
const rpc = require('rage-rpc');
|
||||||
|
|
||||||
const player = mp.players.at(0); // or any player object
|
const player = mp.players.at(0);
|
||||||
|
|
||||||
rbus.sendClient(player, 'getIsRunning').then(running => {
|
rpc.callClient(player, 'getIsClimbing').then(climbing => {
|
||||||
if(running){
|
if(climbing){
|
||||||
console.log('The player is running!');
|
console.log('The player is climbing!');
|
||||||
}else{
|
}else{
|
||||||
console.log('The player is not running!');
|
console.log('The player is not climbing!');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// or even just this inside an async function:
|
// or even just this inside an async function:
|
||||||
const isRunning = await rbus.sendClient(player, 'getIsRunning');
|
const isClimbing = await rpc.callClient(player, 'getIsClimbing');
|
||||||
```
|
|
||||||
|
|
||||||
##### Client-side
|
|
||||||
```javascript
|
|
||||||
const rbus = require('rage-eventbus');
|
|
||||||
|
|
||||||
rbus.on('getIsRunning', () => mp.players.local.isRunning);
|
|
||||||
```
|
```
|
||||||
|
|
||||||
**_That's it!_** No extra code to sort out who is asking for what, or setting up multiple events on each side just to send a single piece of data back to the caller.
|
**_That's it!_** No extra code to sort out who is asking for what, or setting up multiple events on each side just to send a single piece of data back to the caller.
|
||||||
@ -42,30 +41,71 @@ rbus.on('getIsRunning', () => mp.players.local.isRunning);
|
|||||||
|
|
||||||
**Situation:** A CEF instance wants a list of all vehicle license plates directly from the server.
|
**Situation:** A CEF instance wants a list of all vehicle license plates directly from the server.
|
||||||
|
|
||||||
##### Browser
|
##### Server-side
|
||||||
```javascript
|
```javascript
|
||||||
const rbus = require('rage-eventbus');
|
constrpc = require('rage-rpc');
|
||||||
|
|
||||||
rbus.sendServer('getAllLicensePlates').then(plates => {
|
rpc.register('getAllLicensePlates', () => mp.vehicles.toArray().map(vehicle => vehicle.numberPlate));
|
||||||
alert(plates.join(', '));
|
|
||||||
});
|
|
||||||
```
|
```
|
||||||
|
|
||||||
##### Client-side
|
##### Client-side
|
||||||
```javascript
|
```javascript
|
||||||
// even if not using rbus on the client, it must be required somewhere before CEF can send any events
|
// even if not using RPC on the client, it must be required somewhere before CEF can send any events
|
||||||
require('rage-eventbus');
|
require('rage-rpc');
|
||||||
```
|
```
|
||||||
|
|
||||||
##### Server-side
|
##### Browser
|
||||||
```javascript
|
```javascript
|
||||||
const rbus = require('rage-eventbus');
|
const rpc = require('rage-rpc');
|
||||||
|
|
||||||
rbus.on('getAllLicensePlates', () => {
|
rpc.callServer('getAllLicensePlates').then(plates => {
|
||||||
return mp.vehicles.toArray().map(vehicle => vehicle.plate);
|
alert(plates.join(', '));
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
With `rage-eventbus`, CEF can directly communicate with the server and vice-versa.
|
With `rage-rpc`, CEF can directly communicate with the server and vice-versa, without having to pass everything through the client-side JS.
|
||||||
|
|
||||||
###### In vanilla RAGE, you would have to set up multiple events for sending/receiving on the client-side, call them from CEF, then resend the data to the server and back. It's a hassle.
|
###### In vanilla RAGE, you would have to set up multiple events for sending/receiving on the client-side, call them from CEF, then resend the data to the server and back. It's a huge hassle.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Client to Server
|
||||||
|
|
||||||
|
**Situation:** Give the clients/CEF the ability to log to the server's console.
|
||||||
|
|
||||||
|
##### Server-side
|
||||||
|
```javascript
|
||||||
|
const rpc = require('rage-rpc');
|
||||||
|
|
||||||
|
rpc.register('log', (message, info) => {
|
||||||
|
/*
|
||||||
|
the second argument, info, gives information about the request such as
|
||||||
|
- the internal ID of the request
|
||||||
|
- the environment in which the request was sent (server, client, or cef)
|
||||||
|
- the player who sent the request, if any
|
||||||
|
*/
|
||||||
|
|
||||||
|
console.log(info.player.name+': '+message);
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
##### Client-side OR Browser
|
||||||
|
```javascript
|
||||||
|
const rpc = require('rage-rpc');
|
||||||
|
|
||||||
|
function log(message){
|
||||||
|
return rpc.callServer('log', message);
|
||||||
|
}
|
||||||
|
|
||||||
|
// send it and forget it
|
||||||
|
log("Hello, Server!");
|
||||||
|
|
||||||
|
// send it again, but make sure it was successfully received
|
||||||
|
log("Hello again!").then(() => {
|
||||||
|
// the server acknowledged and processed the message
|
||||||
|
}).catch(() => {
|
||||||
|
// the message either timed out or the procedure was never registered
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
Note that once any side of the game registers a procedure, any context can immediately start accessing it. You could call `rpc.callServer('log', message);` from any CEF instance or anywhere in the client without any further setup.
|
||||||
|
Reference in New Issue
Block a user