跳到主要内容

宿主管理

1. 初始化宿主​

未初始化宿主前,宿主状态为 UNAVAILABLE。

TJHostHandle 调用 createGameHandle 初始化宿主,宿主状态转为 WAITING。

接口原型​

// 创建新宿主
hostHandle.createGameHandle()

返回值​

返回 Promise<RuntimeGameHandle> - 游戏运行时句柄

代码示例​

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);
});

平台特性说明​

PC 版本的窗口管理完全由 SDK 内部处理,开发者无需关心窗口的创建、显示和销毁。窗口会在游戏启动时自动创建,在游戏销毁时自动关闭。

2. 销毁宿主​

销毁游戏句柄,释放所有资源。宿主状态转为 DESTROYED。

接口原型​

gameHandle.destroy()

返回值​

返回 Promise<void>

代码示例​

// 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();
});

重要说明​

  1. 资源清理:destroy() 会清理所有游戏相关资源,包括:

    • 游戏窗口
    • 音频引擎
    • 文件系统监听
    • 网络连接
    • 事件监听器
  2. 异步操作:销毁是异步操作,建议使用 await 等待完成

  3. 重复调用:多次调用 destroy() 是安全的,后续调用会被忽略

  4. 窗口管理:销毁游戏句柄时,关联的 Electron 窗口会自动关闭

完整生命周期示例​

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();