H5 files refer to HTML5 pages created by developers for multi-terminal sharing, and are a new file type supported by ZegoDocsView SDK. Developers can add functions such as page turning, skipping, animation triggers, audio and video in H5 files similar to dynamic PPT.
This article will introduce two parts through the production and use of H5 files:
ZEGO provides the completed H5 file for developers' reference, including the integrated ZEGO bridge layer and how to use the interface. For details, please refer to H5 file sample source code.
Please refer to Sample Codes for the complete sample source code of using H5 files in ZegoDocsView SDK.
This sequence diagram describes the interaction process of the H5 file through the ZEGO bridge layer and ZegoDocsView SDK when the developer makes the H5 file.
The production of H5 files, that is, how to integrate and interact with the ZEGO bridge layer, is divided into the following 5 steps. Developers please make sure to follow the steps below.
Introduce the ZEGO bridge layer in the H5 file. Developers need to ensure that the H5 resource has been loaded when using the bridge layer method.
The bridge layer file address is as follows:
https://storage.zego.im/ppt2h5/ZEGOBridge.js
Introduce code examples:
<script src="https://storage.zego.im/ppt2h5/ZEGOBridge.js"></script>
Call getInstance to register related callbacks and get the only instance of the bridge layer.
In order to achieve the effect of multi-terminal interoperability, all business operations that need to be executed in the H5 file must first call the bridge layer interface, and then execute in the corresponding callback.
// Get the unique instance
const bridge = ZEGOBridge.getInstance({
// Register page flip, skip callback
onPageChange: function (params) {
// A Promise object needs to be returned in this callback function to notify the bridge layer that the current page turning and skipping tasks have been completed
return new Promise(resolve => {
// Execute specific page turning and stepping logic
// Specific business logic
resolve();
});
},
// Register operation record callback
onRecordChange: function (params)
{
// Need to return a Promise object in this callback function to notify the bridge layer that the current operation task has been executed
return new Promise(resolve => {
// recordList represents the current operation record list
// oldRecordList last operation record list
// recordType generates the operation type of the current operation record list, and can implement specific services according to this type
const {recordList, oldRecordList, recordType} = params;
// Execute specific business logic according to recordType, recordList, oldRecordList
resolve();
});
},
// Register error callback, optional
onError: function (params) {
// Specific business logic
}
});
Call getReady to notify the bridge layer that the page is ready, and pass in information such as the initial page number, step number, total page number, and current page total step number.
// Pass in the initial number of pages, the number of steps, the total number of pages, all pages correspond to the total number of steps
bridge.getReady({
page: 1, // The initial page number is 1
step: 1, // The initial number of steps on the current page is 1
pageCount: 3, // The total number of pages is 3
maxSteps: [5, 3, 1] // There are 3 pages in total, and the total number of steps on each page is 5, 3, 1 respectively
})
Call requestChangePage to notify the bridge layer that it needs to turn pages and skip steps.
// Request to jump to page 3, step 1
bridge.requestChangePage(3, 1);
Call other related interfaces to notify the bridge layer that other operations are currently required to be performed.
// At this time, add a record with the current page content as id
bridge.pushRecord({ id:'ZEGO' })
// Remove the last operation record
bridge.popRecord()
// Clear the operation record of the current page
bridge.clearRecordStack()
// Replace all operation records on the current page with the record whose content is name
bridge.replaceRecordStack({ name:'ZEGO' })
After the H5 file is successfully created, if you want to achieve multi-terminal interoperability, please follow the steps below:
Please refer to H5 Document Specification for H5 document production and usage specifications.
// Callback parameters for changing the number of steps and pages
interface OnPageChangeParams {
page: number; // current page number, starting from 1
step: number; // current step number, starting from 1
oldPage: number; // Original page number, starting from 1
oldStep: number; // original step number, starting from 1
taskID: string; // task ID
}
// Operation record type
enum RecordType {
Push = 1, // append, corresponding to pushRecord interface
Pop, // remove the latest one, corresponding to the popRecord interface
Replace, // Full replacement, corresponding to the replaceRecordStack interface
Clear, // Clear, corresponding to the clearRecordStack interface
Stop = 100, // Stop audio and video scene
LateJoinRoom = 101, // Back-in room scene
Back = 102 // The operation failed back to the scene
}
// Operation record list change callback parameters
interface OnRecordChangeParams {
recordList?: any[]; // current operation list, does not exist when recordType = 100
oldRecordList?: any[]; // Original operation list, does not exist when recordType = 100
taskID: string; // task ID
recordType: RecordType; // Operation record type
push: boolean; // Is the current operation a remote push
}
// Failed callback parameters
interface OnErrorParams {
code: number; // error code
msg: string; // error description
data?: any; // Business parameters
error?: Error; // Error stack
}
// Callback for changes in steps and pages
type pageChangeCallback = (params: OnPageChangeParams) => Promise<any>;
// Operation record change callback
type recordChangeCallback = (params: OnRecordChangeParams) => Promise<any>;
// Error callback
type errorCallback = (params: OnErrorParams) => void;
// Get the parameters that need to be passed in for the instance
interface BridgeOptions {
onPageChange: pageChangeCallback; // Page number, step number change callback
onRecordChange: recordChangeCallback; // record change callback
onError?: errorCallback; // Error callback, optional
}
// Get ZEGOBridge singleton
static getInstance(options: BridgeOptions): Bridge;
// page initial data
interface InitData {
page: number; // The initial page number of the file, starting from 1
step: number; // The step number of the initial current page of the file, starting from 1
pageCount: number; // The total number of pages of the file
maxSteps: number[]; //The total number of steps corresponding to all pages of the file.
}
/**
* Notify the bridge layer page is ready
*
* Timing of calling: When the script is successfully introduced, the page is ready, and multi-terminal synchronization is possible
*
* note: Generally, it can be called in the event callback of page load
*
* @param data page initial data
*/
getReady(data: InitData): void
/**
* Send page turning, skip request
*
* Timing of calling: when the page is ready, after the call to getReady is completed, when you need to turn pages or skip steps
*
* note: This is just to send page turning and skipping requests, which does not mean that the request is successful. Please perform specific operations in the onPageChange callback
*
* @param page target page number
*
* @param step target number of steps, optional, if not filled, the default is 1, which means to jump to the first step of the current page
*
* @return Request task ID, unique identification
*/
requestChangePage(page: number, step?: number): string
ZEGOBridge provides 4 interfaces to combine page operations. All interfaces are used to update the operation record list recordList of the current page. H5 files need to ensure that the recordList cannot exceed the maximum length limit of the ZEGO bridge layer after the combined call. If the limit is exceeded, the bridge layer will Thrown in the onError callback, the current operation request will be invalid.
Maximum length calculation rule: The number of bytes after the recordList is converted to base64 cannot exceed 948 bytes.
/**
* Add an operation record
*
* Timing of the call: when the page is ready, after the call to getReady is complete, when additional operations are needed
*
* note: This is just to send the request, it does not mean that the request is successful, please perform the specific operation in the onRecordChange callback
*
* note: If the developer needs to pay attention to the operation history, please use this method
*
* @param record operation record
*
* @return Request task ID, unique identification
*/
pushRecord(record: string | number | Array<any> | object | boolean | null): string
/**
* Remove the last operation record
*
* Timing of the call: the page is ready, after the getReady call is completed, when the last operation record needs to be removed
*
* note: This is just to send the request, it does not mean that the request is successful, please perform the specific operation in the onRecordChange callback
*
* @return Request task ID, unique identifier and the most recent operation record
*/
popRecord(): {taskID: string; record: string | number | Array<any> | object | boolean | null}
/**
* Clear the operation record of the current page
*
* Timing of calling: the page is ready, after the call to getReady is completed, when the current page operation record needs to be cleared
*
* note: This is just to send the request, it does not mean that the request is successful, please perform the specific operation in the onRecordChange callback
*
* note: Whenever the number of pages is changed, the script has cleared the operation record. Under normal circumstances, the developer does not need to use this method
*
* @param record operation record
*
* @return Request task ID, unique identification
*/
clearRecordStack(): string
/**
* Replace the current page operation record
*
* Timing of calling: the page is ready, after the call to getReady is completed, when the current page operation record needs to be replaced
*
* note: This is just to send the request, it does not mean that the request is successful, please perform the specific operation in the onRecordChange callback
*
* note: replace the current operation record list in full, if the developer does not need to pay attention to the operation history record, please use this method
*
* @param record operation record
*
* @return Request task ID, unique identification
*/
replaceRecordStack(record: string | number | Array<any> | object | boolean | null): string
For H5 bridge layer error codes, please refer to Common Error Codes.