zoiper5-rpc-api-js-wrapper 中文文档教程

发布于 2年前 浏览 8 更新于 2年前

Zoiper5 RPC API 包装器

用于管理客户端和 Zoiper5 RPC API 服务器之间通信的库。

安装

包管理器

使用 npm:

npm install zoiper5-rpc-api-js-wrapper

使用yarn:

yarn add zoiper5-rpc-api-js-wrapper

导入

安装后,它可以作为 ECMAScript 或 CommonJS 模块导入。

使用 ECMAScript 模块:

import {RPCManager, ConnectionManager} from 'zoiper5-rpc-api-js-wrapper';

使用 CommonJS 模块:

const {RPCManager, ConnectionManager} = require('zoiper5-rpc-api-js-wrapper');

脚本元素

该库依赖于需要首先包含的 simple-jsonrpc-js

使用 jsDelivr CDN (UMD):

<script src="https://cdn.jsdelivr.net/npm/simple-jsonrpc-js@1/dist/simple-jsonrpc-js.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/zoiper5-rpc-api-js-wrapper/dist/zoiper5-rpc-api-js-wrapper.umd.js"></script>

使用 unpkg CDN (UMD):

<script src="https://unpkg.com/simple-jsonrpc-js@1/dist/simple-jsonrpc-js.min.js"></script>
<script src="https://unpkg.com/zoiper5-rpc-api-js-wrapper/dist/zoiper5-rpc-api-js-wrapper.umd.js"></script>

导出可在 Z5RPC 命名空间下使用:

const {RPCManager, ConnectionManager} = Z5RPC;

也可以使用 ECMAScript 模块导入映射的帮助下。

用法

const rpcManager = new RPCManager({
  // Check Zoiper5 API's documentation about `Phone.registerCallback` and `Callbacks` for more information on callback names and signatures.
  'callback-name-1': fn1,
  'callback-name-2': fn2,
});
const connectionManager = new ConnectionManager(rpcManager, {
  url: '<rpc_server_url>',
});
connectionManager.onClose = () => {
  console.log('connection closed');
};

try {
  // Connect to the RPC server.
  await connectionManager.openConnection();
  // Obtain a reference to the instance of class `Phone`.
  const zoiperAPI = await rpcManager.initialize('api-token');
  // Do something with the `Phone` instance...
  console.log('application version:', await zoiperAPI.versionPhone);
  // Close the RPC server connection.
  connectionManager.closeConnection();
} catch (ex) {
  console.error('something went wrong...');
}

TypeScript

该库提供 TypeScript 定义,但它不为 Zoiper5 API 本身提供此类定义。您可以提供自己的:

interface Phone {
  versionPhone: Promise<string>;
}

const rpcManager = new RPCManager<Phone>({});
const zoiperAPI = await rpcManager.initialize('api-token');
zoiperAPI.versionPhone; // `Promise<string>`
zoiperAPI.nonexistent; // `Property 'nonexistent' does not exist on type 'Phone'. ts(2339)`

示例

获取源代码,并检查 examples/ 以获取示例列表及其各自的 README.md 文件以了解更多信息有关他们的信息。

许可证

MIT

Zoiper5 RPC API wrapper

A library for managing the communication between the client and the Zoiper5 RPC API server.

Installation

Package manager

Using npm:

npm install zoiper5-rpc-api-js-wrapper

Using yarn:

yarn add zoiper5-rpc-api-js-wrapper

Importing

Once installed, it can be imported as either ECMAScript or CommonJS module.

Using ECMAScript module:

import {RPCManager, ConnectionManager} from 'zoiper5-rpc-api-js-wrapper';

Using CommonJS module:

const {RPCManager, ConnectionManager} = require('zoiper5-rpc-api-js-wrapper');

Script element

This library depends on simple-jsonrpc-js which needs to be included first.

Using jsDelivr CDN (UMD):

<script src="https://cdn.jsdelivr.net/npm/simple-jsonrpc-js@1/dist/simple-jsonrpc-js.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/zoiper5-rpc-api-js-wrapper/dist/zoiper5-rpc-api-js-wrapper.umd.js"></script>

Using unpkg CDN (UMD):

<script src="https://unpkg.com/simple-jsonrpc-js@1/dist/simple-jsonrpc-js.min.js"></script>
<script src="https://unpkg.com/zoiper5-rpc-api-js-wrapper/dist/zoiper5-rpc-api-js-wrapper.umd.js"></script>

Exports are available under the Z5RPC namespace:

const {RPCManager, ConnectionManager} = Z5RPC;

It's also possible to use the ECMAScript module with the help of import maps.

Usage

const rpcManager = new RPCManager({
  // Check Zoiper5 API's documentation about `Phone.registerCallback` and `Callbacks` for more information on callback names and signatures.
  'callback-name-1': fn1,
  'callback-name-2': fn2,
});
const connectionManager = new ConnectionManager(rpcManager, {
  url: '<rpc_server_url>',
});
connectionManager.onClose = () => {
  console.log('connection closed');
};

try {
  // Connect to the RPC server.
  await connectionManager.openConnection();
  // Obtain a reference to the instance of class `Phone`.
  const zoiperAPI = await rpcManager.initialize('api-token');
  // Do something with the `Phone` instance...
  console.log('application version:', await zoiperAPI.versionPhone);
  // Close the RPC server connection.
  connectionManager.closeConnection();
} catch (ex) {
  console.error('something went wrong...');
}

TypeScript

This library provides TypeScript definitions, but it doesn't provide such for Zoiper5 API itself. You can provide your own:

interface Phone {
  versionPhone: Promise<string>;
}

const rpcManager = new RPCManager<Phone>({});
const zoiperAPI = await rpcManager.initialize('api-token');
zoiperAPI.versionPhone; // `Promise<string>`
zoiperAPI.nonexistent; // `Property 'nonexistent' does not exist on type 'Phone'. ts(2339)`

Examples

Obtain the source code, and check examples/ for a list of examples and their respective README.md file for more information about them.

License

MIT

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文