SDK Initialization
1 Initialize
Initialize the host SDK, which is the first step in using the SDK.
1.1 Interface Prototype
TJHostHandle.initialize(sdkKey, sdkSecret, completion)
1.2 Parameter Description
| parameter | Data Type | Required | Attribute Description |
|---|---|---|---|
sdkKey | String | Yes | SDK Key |
sdkSecret | String | Yes | SDK Key |
completion | Function | Yes | Initialization completion callback(hostHandle, error) => {} |
1.3 Code Example
const { TJHostHandle } = require('webglhost-runtime-pc');
TJHostHandle.initialize('your-sdk-key', 'your-sdk-secret', (hostHandle, error) => {
if (error) {
console.error('SDK initialization failed:', error);
return;
}
console.log('SDK initialized successfully');
// 保存 hostHandle 以便后续使用
this.hostHandle = hostHandle;
});
2 Obtain Host Version Number
Get the version information of the current host SDK.
2.1 Interface Prototype
TJHostHandle.getVersionName()
TJHostHandle.getVersionCode()
2.2 Code Example
const versionName = TJHostHandle.getVersionName(); // e.g., "1.0.0"
const versionCode = TJHostHandle.getVersionCode(); // e.g., 1
console.log(`Host Version: ${versionName} (${versionCode})`);
3 Obtain the host commit ID
Get the commit ID of the current host SDK for version tracking.
3.1 Interface Prototype
TJHostHandle.getCommitId()
3.2 Code Example
const commitId = TJHostHandle.getCommitId();
console.log(`Host Commit ID: ${commitId}`);
4 Complete Initialization Example
const { TJHostHandle, HostRuntimeError } = require('webglhost-runtime-pc');
class GameHost {
constructor() {
this.hostHandle = null;
}
async initialize() {
return new Promise((resolve, reject) => {
TJHostHandle.initialize(
process.env.SDK_KEY,
process.env.SDK_SECRET,
(hostHandle, error) => {
if (error) {
console.error('SDK initialization failed:', error.code, error.message);
reject(error);
return;
}
this.hostHandle = hostHandle;
console.log('SDK initialized successfully');
console.log(`Version: ${TJHostHandle.getVersionName()}`);
console.log(`Commit: ${TJHostHandle.getCommitId()}`);
resolve(hostHandle);
}
);
});
}
}
// Usage
const gameHost = new GameHost();
gameHost.initialize()
.then(() => {
console.log('Ready to create game handles');
})
.catch(error => {
console.error('Initialization error:', error);
});