Skip to main content

Host Management

1 Initialize Host​

Before initializing the host, the host status is UNAVAILABLE.

TJHostHandle calls createGameHandle to initialize the host, and the host state changes to WAITING.

1.1 Interface Prototype​

// create hostHandle
hostHandle.createGameHandle()

1.2 Return Value​

Returns Promise<RuntimeGameHandle> - Runtime Game Handle

1.3 Code Example​

const { TJHostHandle } = require('webglhost-runtime-pc');

class GameManager {
constructor() {
this.hostHandle = null;
this.gameHandle = null;
}

async initializeHost() {
// First initialize SDK
await new Promise((resolve, reject) => {
TJHostHandle.initialize('sdk-key', 'sdk-secret', (hostHandle, error) => {
if (error) {
reject(error);
return;
}
this.hostHandle = hostHandle;
resolve();
});
});

// Then create game handle
try {
this.gameHandle = await this.hostHandle.createGameHandle();
console.log('Game handle created successfully');
} catch (error) {
console.error('Failed to create game handle:', error);
throw error;
}
}
}

// Usage
const gameManager = new GameManager();
gameManager.initializeHost()
.then(() => {
console.log('Host initialized, ready to start game');
})
.catch(error => {
console.error('Host initialization failed:', error);
});

1.4 Platform Feature Description​

Window management for the PC version is fully handled internally by the SDK, so developers do not need to worry about window creation, display, and destruction. Windows will be automatically created when the game starts and automatically closed when the game is destroyed.

2 Destroy Host​

Destroy the game handle and release all resources. The host state changes to DESTROYED.

2.1 Interface Prototype​

gameHandle.destroy()

2.2 Return Value​

ReturnsPromise<void>

2.3 Code Example​

// Destroy game handle when done
async function cleanupGame() {
if (gameHandle) {
try {
await gameHandle.destroy();
console.log('Game handle destroyed successfully');
gameHandle = null;
} catch (error) {
console.error('Failed to destroy game handle:', error);
}
}
}

// Call when application is closing
process.on('beforeExit', async () => {
await cleanupGame();
});

2.4 Important Instructions​

  1. Resource Cleanup: destroy() will clean up all game-related resources, including:

    • Game Window

    • Audio Engine

    • File System Monitoring

    • Network Connection

    • Event Listener

  2. Asynchronous Operation : Destruction is an asynchronous operation, it is recommended to use await to wait for completion

  1. Repeated calls: Multiple calls to destroy() are safe, and subsequent calls will be ignored

  2. Window Management: When the game handle is destroyed, the associated Electron window will automatically close

3 Complete Lifecycle Example​

const { TJHostHandle } = require('webglhost-runtime-pc');

class GameLifecycleManager {
constructor() {
this.hostHandle = null;
this.gameHandle = null;
this.isInitialized = false;
}

// Initialize SDK and create game handle
async initialize(sdkKey, sdkSecret) {
if (this.isInitialized) {
console.warn('Already initialized');
return;
}

// Step 1: Initialize SDK
await new Promise((resolve, reject) => {
TJHostHandle.initialize(sdkKey, sdkSecret, (hostHandle, error) => {
if (error) {
reject(error);
return;
}
this.hostHandle = hostHandle;
resolve();
});
});

// Step 2: Create game handle
this.gameHandle = await this.hostHandle.createGameHandle();
this.isInitialized = true;

console.log('Lifecycle manager initialized');
}

// Clean up all resources
async cleanup() {
if (!this.isInitialized) {
return;
}

if (this.gameHandle) {
await this.gameHandle.destroy();
this.gameHandle = null;
}

this.hostHandle = null;
this.isInitialized = false;

console.log('Lifecycle manager cleaned up');
}

// Get current game handle
getGameHandle() {
if (!this.isInitialized || !this.gameHandle) {
throw new Error('Game handle not initialized');
}
return this.gameHandle;
}
}

// Usage example
const lifecycleManager = new GameLifecycleManager();

async function main() {
try {
// Initialize
await lifecycleManager.initialize('sdk-key', 'sdk-secret');

// Get game handle and use it
const gameHandle = lifecycleManager.getGameHandle();

// ... use game handle for game operations ...

// Cleanup when done
await lifecycleManager.cleanup();
} catch (error) {
console.error('Error in game lifecycle:', error);
}
}

main();