Skip to content

Commit

Permalink
Fix async connection method in RpgClientEngine
Browse files Browse the repository at this point in the history
  • Loading branch information
RSamaium committed Dec 5, 2023
1 parent 8af2fff commit 1a289fa
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 19 deletions.
6 changes: 3 additions & 3 deletions packages/client/src/RpgClientEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ export class RpgClientEngine {
}
// @ts-ignore
const envUrl = this.envs.VITE_SERVER_URL
this.connection(
await this.connection(
serverUri.url ? serverUri.url + ':' + serverUri.port :
envUrl ? envUrl : undefined
)
Expand Down Expand Up @@ -427,7 +427,7 @@ export class RpgClientEngine {
* @returns {void}
* @memberof RpgClientEngine
*/
connection(uri?: string) {
async connection(uri?: string) {
const { standalone } = this.gameEngine

this._serverUrl = uri || ''
Expand Down Expand Up @@ -662,7 +662,7 @@ export class RpgClientEngine {
RpgGui._setSocket(this.socket)

if (standalone) {
this.socket.connection({
await this.socket.connection({
auth: {
token: this.session
}
Expand Down
1 change: 1 addition & 0 deletions packages/common/src/Plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type Plugin = PluginSides | [PluginSides, any]
export enum HookServer {
Start = 'Server.Start',
Step = "Server.Step",
Auth = "Server.Auth",
PlayerConnected = 'Server.onConnected',
PlayerDisconnected = 'Server.onDisconnected',
AddMap = 'Server.AddMap',
Expand Down
3 changes: 3 additions & 0 deletions packages/sample2/main/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { RpgServerEngine } from "@rpgjs/server"

export default {
auth(engine: RpgServerEngine, socket) {

},
onStart(engine: RpgServerEngine) {

}
Expand Down
10 changes: 9 additions & 1 deletion packages/server/src/RpgServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ export interface RpgServerEngineHooks {
* @memberof RpgServerEngineHooks
*/
onStep?: (server: RpgServerEngine) => any

/**
*
*
* @param server
* @param socket
* @returns
*/
auth?: (server: RpgServerEngine, socket: any) => Promise<string> | string | never
}

export interface RpgPlayerHooks {
Expand Down Expand Up @@ -239,7 +248,6 @@ export interface RpgServer {
player?: string[],
engine?: string[]
}

/**
* Adding sub-modules
*
Expand Down
3 changes: 2 additions & 1 deletion packages/server/src/entry-point.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ export default async function (modules: ModuleType[], options: RpgServerEntryPoi

const relationsEngine = {
onStart: HookServer.Start,
onStep: HookServer.Step
onStep: HookServer.Step,
auth: HookServer.Auth
}

const { playerProps } = await loadModules(modules, {
Expand Down
11 changes: 7 additions & 4 deletions packages/server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,15 @@ export class RpgServerEngine {
}

private transport(io): Transport {
const timeoutDisconnect = 0
const timeoutDisconnect = this.globalConfig.timeoutDisconnect ?? 0
const transport = new Transport(io, {
timeoutDisconnect,
auth: (socket) => {
//console.log( socket.handshake.auth)
return Utils.generateUID()
auth: async (socket) => {
const val = await RpgPlugin.emit(HookServer.Auth, [this, socket], true)
if (val.length == 0) {
return Utils.generateUID()
}
return val[val.length-1]
}
})
this.world.timeoutDisconnect = timeoutDisconnect
Expand Down
16 changes: 7 additions & 9 deletions packages/standalone/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/standalone/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"@rpgjs/client": "^4.1.3",
"@rpgjs/common": "^4.1.3",
"@rpgjs/server": "^4.1.3",
"simple-room": "^3.0.5"
"simple-room": "^3.0.6"
},
"devDependencies": {
"@rpgjs/compiler": "^4.1.3",
Expand Down
76 changes: 76 additions & 0 deletions tests/unit-tests/specs/server-hooks.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import {_beforeEach} from './beforeEach'
import { RpgMap, RpgModule, RpgPlayer, RpgServer, RpgServerEngine } from '@rpgjs/server'
import { clear, testing } from '@rpgjs/testing'
import { test, afterEach, expect, vi, describe } from 'vitest'

test('Test onStart Hook', async () => {
const onStart = vi.fn()

@RpgModule<RpgServer>({
engine: {
onStart
}
})
class RpgServerModule {}

await _beforeEach([{
server: RpgServerModule
}])

expect(onStart).toHaveBeenCalled()
expect(onStart).toHaveBeenCalledWith(expect.any(RpgServerEngine))
})

test('Test onStep Hook', async () => {
const onStep = vi.fn()

@RpgModule<RpgServer>({
engine: {
onStep
}
})
class RpgServerModule {}

await _beforeEach([{
server: RpgServerModule
}])

expect(onStep).toHaveBeenCalled()
expect(onStep).toHaveBeenCalledWith(expect.any(RpgServerEngine))
})

describe('Test auth Hook', () => {

async function createAuthTest(auth) {

@RpgModule<RpgServer>({
engine: {
auth
}
})
class RpgServerModule {}

const { player } = await _beforeEach([{
server: RpgServerModule
}])

expect(auth).toHaveBeenCalled()
expect(auth).toHaveBeenCalledWith(expect.any(RpgServerEngine), expect.anything())
expect(auth).toHaveReturnedWith('test')
expect(player.id).toBe('test')
}

test('Test auth Hook', async () => {
const auth = vi.fn().mockReturnValue('test')
await createAuthTest(auth)
})

test('Test auth Hook with Promise', async () => {
const auth = vi.fn().mockResolvedValue('test')
await createAuthTest(auth)
})
})

afterEach(() => {
clear()
})

0 comments on commit 1a289fa

Please sign in to comment.