Rpc integration + type fixes #3
93
rpc/readme.md
Normal file
93
rpc/readme.md
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
# Rage-FW-RPC
|
||||||
|
is an all-in package with asynchronous RPC implementation for RageMP servers in JS/TS
|
||||||
|
|
||||||
|
# Installation
|
||||||
|
``` shell
|
||||||
|
npm i rage-fw-rpc
|
||||||
|
```
|
||||||
|
```shell
|
||||||
|
pnpm i rage-fw-rpc
|
||||||
|
```
|
||||||
|
```shell
|
||||||
|
yarn add rage-fw-rpc
|
||||||
|
```
|
||||||
|
|
||||||
|
Import installed package and initialize rpc:
|
||||||
|
```ts
|
||||||
|
// lib/rpc.js
|
||||||
|
|
||||||
|
import { Rpc } from 'rage-fw-rpc'
|
||||||
|
export const rpc = new Rpc(/* options */)
|
||||||
|
```
|
||||||
|
|
||||||
|
# Features
|
||||||
|
- Type-safe events via [TS generics](https://www.typescriptlang.org/docs/handbook/2/generics.html), avoiding type wrappers
|
||||||
|
- Built-in logging options for each environment
|
||||||
|
- Error-safe developer mode for browser
|
||||||
|
- Calls can receive response from called environments via Promises (browser -> server -> browser, etc.)
|
||||||
|
- Actual human-readable errors
|
||||||
|
|
||||||
|
# Docs
|
||||||
|
*Extended version with details coming soon*
|
||||||
|
|
||||||
|
## register
|
||||||
|
Registers a callback function for a specified event
|
||||||
|
```ts
|
||||||
|
rpc.register('playerJoin', (player) => {
|
||||||
|
console.log(`Connected: ${player.socialClub}`)
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
## unregister
|
||||||
|
Unregisters callback function for a specified event
|
||||||
|
```ts
|
||||||
|
rpc.unregister('playerDamage')
|
||||||
|
```
|
||||||
|
|
||||||
|
## callClient
|
||||||
|
Calls a client-side event from server or browser
|
||||||
|
|
||||||
|
From browser:
|
||||||
|
```ts
|
||||||
|
rpc.callClient('updatePlayerData', ['argument']).then(response => {
|
||||||
|
console.log(`Received: ${response}`)
|
||||||
|
})
|
||||||
|
```
|
||||||
|
From server (requires player):
|
||||||
|
```ts
|
||||||
|
rpc.callClient(player, 'updatePlayerData', ['argument']).then(response => {
|
||||||
|
console.log(`Received: ${response}`)
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
## callServer
|
||||||
|
Calls a server-side event from browser or client
|
||||||
|
```ts
|
||||||
|
rpc.callServer('updatePlayerData', ['argument']).then(response => {
|
||||||
|
console.log(`Received: ${response}`)
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
## callBrowser
|
||||||
|
Calls a server-side event from browser or client
|
||||||
|
|
||||||
|
From client:
|
||||||
|
```ts
|
||||||
|
rpc.callBrowser('updatePlayerData', ['argument']).then(response => {
|
||||||
|
console.log(`Received: ${response}`)
|
||||||
|
})
|
||||||
|
```
|
||||||
|
From client (requires player):
|
||||||
|
```ts
|
||||||
|
rpc.callBrowser(player, 'updatePlayerData', ['argument']).then(response => {
|
||||||
|
console.log(`Received: ${response}`)
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
## call
|
||||||
|
Calls an event in current environment
|
||||||
|
```ts
|
||||||
|
rpc.call('triggerSomething').then(response => {
|
||||||
|
console.log(`Received: ${response}`)
|
||||||
|
})
|
||||||
|
```
|
@ -263,7 +263,7 @@ class Rpc extends Wrapper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calls a server-side event from server or client
|
* Calls a server-side event from browser or client
|
||||||
*
|
*
|
||||||
* @template Arguments - An array of argument types to be passed to the server event
|
* @template Arguments - An array of argument types to be passed to the server event
|
||||||
* @template EventName - A string representing the server event name or union of names
|
* @template EventName - A string representing the server event name or union of names
|
||||||
@ -438,8 +438,8 @@ class Rpc extends Wrapper {
|
|||||||
* @returns {Promise<Return>} A promise resolving to the return value of the event
|
* @returns {Promise<Return>} A promise resolving to the return value of the event
|
||||||
*
|
*
|
||||||
* @example
|
* @example
|
||||||
* // Calls an event on browser without specifying a player
|
* // Calls an event in current environment
|
||||||
* callBrowser<[], string, number>('getSomething').then(response => {
|
* call<[], string, number>('getSomething').then(response => {
|
||||||
* console.log(`Received: ${response}`) // ^ number
|
* console.log(`Received: ${response}`) // ^ number
|
||||||
* })
|
* })
|
||||||
*/
|
*/
|
||||||
|
Loading…
Reference in New Issue
Block a user