upd | callServer

This commit is contained in:
Danya H 2024-09-15 19:59:27 +01:00
parent ce5824db81
commit faf4ca3c47
3 changed files with 76 additions and 4 deletions

View File

@ -13,7 +13,9 @@ export class Client {
case utils.environment.CLIENT: case utils.environment.CLIENT:
return this.executeLocal(data) return this.executeLocal(data)
case utils.environment.CEF: case utils.environment.CEF:
// transfer to cef // todo transfer to cef
case utils.environment.SERVER:
// todo transfer to server
} }
} }

View File

@ -2,6 +2,7 @@ import { RPC_LISTENER } from './events.ts'
import { utils } from './utils.ts' import { utils } from './utils.ts'
import { client } from './client.ts' import { client } from './client.ts'
import { server } from './server.ts'
import type { RPCData } from './types.ts' import type { RPCData } from './types.ts'
const environment = utils.getEnvironment() const environment = utils.getEnvironment()
@ -9,7 +10,7 @@ const environment = utils.getEnvironment()
export const state = export const state =
environment === utils.environment.CEF ? window.rpcEvents : global.rpcEvents environment === utils.environment.CEF ? window.rpcEvents : global.rpcEvents
class frameworkRpc { class FrameworkRpc {
constructor() { constructor() {
mp.events.add(RPC_LISTENER, async (player: any, data: string) => { mp.events.add(RPC_LISTENER, async (player: any, data: string) => {
switch (environment) { switch (environment) {
@ -21,6 +22,8 @@ class frameworkRpc {
return client.listen(player) return client.listen(player)
case utils.environment.SERVER: case utils.environment.SERVER:
return server.listen(player, data)
case utils.environment.CEF: case utils.environment.CEF:
} }
}) })
@ -39,7 +42,7 @@ class frameworkRpc {
eventName: string, eventName: string,
...args: Args ...args: Args
): Promise<Return | unknown> { ): Promise<Return | unknown> {
return new Promise((resolve, reject) => { return new Promise((resolve, _reject) => {
const uuid = utils.generateUUID() const uuid = utils.generateUUID()
const data: RPCData = { const data: RPCData = {
@ -51,8 +54,45 @@ class frameworkRpc {
} }
player.call(RPC_LISTENER, [utils.stringifyData(data)]) player.call(RPC_LISTENER, [utils.stringifyData(data)])
const responseEventName = utils.generateResponseEventName(uuid)
const handler = (_player: any, data: string) => {
resolve(utils.parseData(data).data)
mp.events.remove(responseEventName)
}
mp.events.add(responseEventName, handler)
})
}
public callServer<Args extends any[] = unknown[], Return = unknown>(
eventName: string,
...args: Args
): Promise<Return | unknown> {
return new Promise((resolve, _reject) => {
const uuid = utils.generateUUID()
const data: RPCData = {
uuid,
eventName,
from: environment,
to: utils.environment.CLIENT,
data: args,
}
mp.events.callRemote(RPC_LISTENER, utils.stringifyData(data))
const responseEventName = utils.generateResponseEventName(uuid)
const handler = (_player: any, data: string) => {
resolve(utils.parseData(data).data)
mp.events.remove(responseEventName)
}
mp.events.add(responseEventName, handler)
}) })
} }
} }
const test = new frameworkRpc() const test = new FrameworkRpc()

30
src/server.ts Normal file
View File

@ -0,0 +1,30 @@
import { utils } from './utils.ts'
import { state } from './index.ts'
import type { RPCData } from './types.ts'
export class Server {
public async listen(player: any, data: string) {
const parsedData = utils.parseData(data)
await this.executeLocal(player, parsedData)
}
private async executeLocal(player: any, data: RPCData) {
const fnResponse = await state[data.eventName](...data.data)
const response = {
data: fnResponse,
...data,
}
this.sendResponseToClient(player, response)
}
private sendResponseToClient(player: any, data: RPCData) {
const eventName = utils.generateResponseEventName(data.uuid)
const prepareForTransfer = utils.stringifyData(data)
player.call(eventName, prepareForTransfer)
}
}
export const server = new Server()