未验证 提交 234bee6a 编写于 作者: O openharmony_ci 提交者: Gitee

!23448 ArkTS整改

Merge pull request !23448 from Lixiaoying25/master
...@@ -180,7 +180,7 @@ IPC/RPC的主要工作是让运行在不同进程的Proxy和Stub互相通信, ...@@ -180,7 +180,7 @@ IPC/RPC的主要工作是让运行在不同进程的Proxy和Stub互相通信,
1. 添加依赖 1. 添加依赖
```ts ```ts
import rpc from "@ohos.rpc"; import rpc from '@ohos.rpc';
// 仅FA模型需要导入@ohos.ability.featureAbility // 仅FA模型需要导入@ohos.ability.featureAbility
// import featureAbility from "@ohos.ability.featureAbility"; // import featureAbility from "@ohos.ability.featureAbility";
``` ```
...@@ -188,17 +188,20 @@ IPC/RPC的主要工作是让运行在不同进程的Proxy和Stub互相通信, ...@@ -188,17 +188,20 @@ IPC/RPC的主要工作是让运行在不同进程的Proxy和Stub互相通信,
Stage模型需要获取context Stage模型需要获取context
```ts ```ts
import Ability from "@ohos.app.ability.UIAbility"; import UIAbility from '@ohos.app.ability.UIAbility';
import Want from '@ohos.app.ability.Want';
import AbilityConstant from '@ohos.app.ability.AbilityConstant';
import window from '@ohos.window';
export default class MainAbility extends Ability { export default class MainAbility extends UIAbility {
onCreate(want, launchParam) { onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
console.log("[Demo] MainAbility onCreate"); console.log("[Demo] MainAbility onCreate");
globalThis.context = this.context; let context = this.context;
} }
onDestroy() { onDestroy() {
console.log("[Demo] MainAbility onDestroy"); console.log("[Demo] MainAbility onDestroy");
} }
onWindowStageCreate(windowStage) { onWindowStageCreate(windowStage: window.WindowStage) {
// Main window is created, set main page for this ability // Main window is created, set main page for this ability
console.log("[Demo] MainAbility onWindowStageCreate"); console.log("[Demo] MainAbility onWindowStageCreate");
} }
...@@ -222,53 +225,62 @@ IPC/RPC的主要工作是让运行在不同进程的Proxy和Stub互相通信, ...@@ -222,53 +225,62 @@ IPC/RPC的主要工作是让运行在不同进程的Proxy和Stub互相通信,
首先,构造变量want,指定要绑定的Ability所在应用的包名、组件名,如果是跨设备的场景,还需要绑定目标设备NetworkId(组网场景下对应设备的标识符,可以使用deviceManager获取目标设备的NetworkId);然后,构造变量connect,指定绑定成功、绑定失败、断开连接时的回调函数;最后,FA模型使用featureAbility提供的接口绑定Ability,Stage模型通过context获取服务后用提供的接口绑定Ability。 首先,构造变量want,指定要绑定的Ability所在应用的包名、组件名,如果是跨设备的场景,还需要绑定目标设备NetworkId(组网场景下对应设备的标识符,可以使用deviceManager获取目标设备的NetworkId);然后,构造变量connect,指定绑定成功、绑定失败、断开连接时的回调函数;最后,FA模型使用featureAbility提供的接口绑定Ability,Stage模型通过context获取服务后用提供的接口绑定Ability。
```ts ```ts
import rpc from "@ohos.rpc";
// 仅FA模型需要导入@ohos.ability.featureAbility // 仅FA模型需要导入@ohos.ability.featureAbility
// import featureAbility from "@ohos.ability.featureAbility"; // import featureAbility from "@ohos.ability.featureAbility";
import rpc from '@ohos.rpc';
import Want from '@ohos.app.ability.Want';
import common from '@ohos.app.ability.common';
import deviceManager from '@ohos.distributedHardware.deviceManager';
let proxy = null; let proxy: rpc.RemoteProxy;
let connectId = null; let connectId: number;
// 单个设备绑定Ability // 单个设备绑定Ability
let want = { let want: Want = {
// 包名和组件名写实际的值 // 包名和组件名写实际的值
"bundleName": "ohos.rpc.test.server", bundleName: "ohos.rpc.test.server",
"abilityName": "ohos.rpc.test.server.ServiceAbility", abilityName: "ohos.rpc.test.server.ServiceAbility",
}; };
let connect = { let connect: common.ConnectOptions = {
onConnect:function(elementName, remote) { onConnect: (elementName, remote: rpc.RemoteProxy) => {
proxy = remote; proxy = remote;
}, },
onDisconnect:function(elementName) { onDisconnect: (elementName) => {
}, },
onFailed:function() { onFailed: () => {
proxy = null; proxy;
} }
}; };
// FA模型使用此方法连接服务 // FA模型使用此方法连接服务
// connectId = featureAbility.connectAbility(want, connect); // connectId = featureAbility.connectAbility(want, connect);
connectId = globalThis.context.connectServiceExtensionAbility(want,connect); connectId = this.context.connectServiceExtensionAbility(want,connect);
// 如果是跨设备绑定,可以使用deviceManager获取目标设备NetworkId // 跨设备绑定
import deviceManager from '@ohos.distributedHardware.deviceManager';
function deviceManagerCallback(deviceManager) {
let deviceList = deviceManager.getTrustedDeviceListSync();
let networkId = deviceList[0].networkId;
let want = {
"bundleName": "ohos.rpc.test.server",
"abilityName": "ohos.rpc.test.service.ServiceAbility",
"networkId": networkId,
"flags": 256
};
// 建立连接后返回的Id需要保存下来,在断开连接时需要作为参数传入
// FA模型使用此方法连接服务
// connectId = featureAbility.connectAbility(want, connect);
connectId = globalThis.context.connectServiceExtensionAbility(want,connect);
}
// 第一个参数是本应用的包名,第二个参数是接收deviceManager的回调函数 // 第一个参数是本应用的包名,第二个参数是接收deviceManager的回调函数
deviceManager.createDeviceManager("ohos.rpc.test", deviceManagerCallback); deviceManager.createDeviceManager("ohos.rpc.test", (err: Error, data: deviceManager.DeviceManager) => {
if (err) {
console.error("createDeviceManager errCode:" + err.code + ",errMessage:" + err.message);
return;
}
console.info("createDeviceManager success");
let dmInstance = data;
});
// 使用deviceManager获取目标设备NetworkId
let deviceList: Array<deviceManager.DeviceInfo> = dmInstance.getTrustedDeviceListSync();
let networkId: string = deviceList[0].networkId;
let want: Want = {
bundleName: "ohos.rpc.test.server",
abilityName: "ohos.rpc.test.service.ServiceAbility",
deviceId: networkId,
flags: 256
};
// 建立连接后返回的Id需要保存下来,在断开连接时需要作为参数传入
// FA模型使用此方法连接服务
// connectId = featureAbility.connectAbility(want, connect);
connectId = this.context.connectServiceExtensionAbility(want,connect);
``` ```
...@@ -278,14 +290,14 @@ IPC/RPC的主要工作是让运行在不同进程的Proxy和Stub互相通信, ...@@ -278,14 +290,14 @@ IPC/RPC的主要工作是让运行在不同进程的Proxy和Stub互相通信,
```ts ```ts
onConnect(want: Want) { onConnect(want: Want) {
var robj:rpc.RemoteObject = new Stub("rpcTestAbility"); const robj: rpc.RemoteObject = new Stub("rpcTestAbility");
return robj; return robj;
} }
class Stub extends rpc.RemoteObject { class Stub extends rpc.RemoteObject {
constructor(descriptor) { constructor(descriptor: string) {
super(descriptor); super(descriptor);
} }
onRemoteMessageRequest(code, data, reply, option) { onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, option: rpc.MessageOption): boolean | Promise<boolean> {
// 根据code处理客户端的请求 // 根据code处理客户端的请求
return true; return true;
} }
...@@ -297,20 +309,21 @@ IPC/RPC的主要工作是让运行在不同进程的Proxy和Stub互相通信, ...@@ -297,20 +309,21 @@ IPC/RPC的主要工作是让运行在不同进程的Proxy和Stub互相通信,
客户端在onConnect回调里接收到代理对象,调用sendRequest方法发起请求,在期约(JavaScript期约:用于表示一个异步操作的最终完成或失败及其结果值)或者回调函数里接收结果。 客户端在onConnect回调里接收到代理对象,调用sendRequest方法发起请求,在期约(JavaScript期约:用于表示一个异步操作的最终完成或失败及其结果值)或者回调函数里接收结果。
```ts ```ts
import rpc from '@ohos.rpc';
// 使用期约 // 使用期约
let option = new rpc.MessageOption(); let option = new rpc.MessageOption();
let data = rpc.MessageParcel.create(); let data = rpc.MessageParcel.create();
let reply = rpc.MessageParcel.create(); let reply = rpc.MessageParcel.create();
// 往data里写入参数 // 往data里写入参数
proxy.sendRequest(1, data, reply, option) proxy.sendRequest(1, data, reply, option)
.then(function(result) { .then((result: rpc.SendRequestResult) => {
if (result.errCode != 0) { if (result.errCode != 0) {
console.error("send request failed, errCode: " + result.errCode); console.error("send request failed, errCode: " + result.errCode);
return; return;
} }
// 从result.reply里读取结果 // 从result.reply里读取结果
}) })
.catch(function(e) { .catch((e: Error) => {
console.error("send request got exception: " + e); console.error("send request got exception: " + e);
}) })
.finally(() => { .finally(() => {
...@@ -319,7 +332,7 @@ IPC/RPC的主要工作是让运行在不同进程的Proxy和Stub互相通信, ...@@ -319,7 +332,7 @@ IPC/RPC的主要工作是让运行在不同进程的Proxy和Stub互相通信,
}) })
// 使用回调函数 // 使用回调函数
function sendRequestCallback(result) { function sendRequestCallback(result: rpc.SendRequestResult) {
try { try {
if (result.errCode != 0) { if (result.errCode != 0) {
console.error("send request failed, errCode: " + result.errCode); console.error("send request failed, errCode: " + result.errCode);
...@@ -352,7 +365,7 @@ IPC/RPC的主要工作是让运行在不同进程的Proxy和Stub互相通信, ...@@ -352,7 +365,7 @@ IPC/RPC的主要工作是让运行在不同进程的Proxy和Stub互相通信,
// FA模型使用此方法断开连接 // FA模型使用此方法断开连接
// featureAbility.disconnectAbility(connectId, disconnectCallback); // featureAbility.disconnectAbility(connectId, disconnectCallback);
globalThis.context.disconnectServiceExtensionAbility(connectId); this.context.disconnectServiceExtensionAbility(connectId);
``` ```
## 相关实例 ## 相关实例
......
...@@ -93,17 +93,21 @@ result = object->RemoveDeathRecipient(deathRecipient); // 移除消亡通知 ...@@ -93,17 +93,21 @@ result = object->RemoveDeathRecipient(deathRecipient); // 移除消亡通知
Stage模型在连接服务前需要先获取context Stage模型在连接服务前需要先获取context
```ts ```ts
import Ability from "@ohos.app.ability.UIAbility"; import UIAbility from '@ohos.app.ability.UIAbility';
import Want from '@ohos.app.ability.Want';
import AbilityConstant from '@ohos.app.ability.AbilityConstant';
import window from '@ohos.window';
export default class MainAbility extends Ability {
onCreate(want, launchParam) { export default class MainAbility extends UIAbility {
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
console.log("[Demo] MainAbility onCreate"); console.log("[Demo] MainAbility onCreate");
globalThis.context = this.context; let context = this.context;
} }
onDestroy() { onDestroy() {
console.log("[Demo] MainAbility onDestroy"); console.log("[Demo] MainAbility onDestroy");
} }
onWindowStageCreate(windowStage) { onWindowStageCreate(windowStage: window.WindowStage) {
// Main window is created, set main page for this ability // Main window is created, set main page for this ability
console.log("[Demo] MainAbility onWindowStageCreate"); console.log("[Demo] MainAbility onWindowStageCreate");
} }
...@@ -127,36 +131,42 @@ export default class MainAbility extends Ability { ...@@ -127,36 +131,42 @@ export default class MainAbility extends Ability {
```ts ```ts
// 仅FA模型需要导入@ohos.ability.featureAbility // 仅FA模型需要导入@ohos.ability.featureAbility
// import FA from "@ohos.ability.featureAbility"; // import FA from "@ohos.ability.featureAbility";
import Want from '@ohos.app.ability.Want';
import common from '@ohos.app.ability.common';
let proxy; let proxy: rpc.RemoteProxy;
let connect = { let connect: common.ConnectOptions = {
onConnect: function(elementName, remoteProxy) { onConnect: (elementName, remoteProxy: rpc.RemoteProxy) => {
console.log("RpcClient: js onConnect called."); console.log("RpcClient: js onConnect called.");
proxy = remoteProxy; proxy = remoteProxy;
}, },
onDisconnect: function(elementName) { onDisconnect: (elementName) => {
console.log("RpcClient: onDisconnect"); console.log("RpcClient: onDisconnect");
}, },
onFailed: function() { onFailed: () => {
console.log("RpcClient: onFailed"); console.log("RpcClient: onFailed");
} }
}; };
let want = { let want: Want = {
"bundleName": "com.ohos.server", bundleName: "com.ohos.server",
"abilityName": "com.ohos.server.EntryAbility", abilityName: "com.ohos.server.EntryAbility",
}; };
// FA模型通过此方法连接服务 // FA模型通过此方法连接服务
// FA.connectAbility(want, connect); // FA.connectAbility(want, connect);
globalThis.context.connectServiceExtensionAbility(want, connect); this.context.connectServiceExtensionAbility(want, connect);
```
上述onConnect回调函数中的proxy对象需要等ability异步连接成功后才会被赋值,然后才可调用proxy对象的unregisterDeathRecipient接口方法注销死亡回调
class MyDeathRecipient { ```ts
class MyDeathRecipient implements rpc.DeathRecipient{
onRemoteDied() { onRemoteDied() {
console.log("server died"); console.log("server died");
} }
} }
let deathRecipient = new MyDeathRecipient(); let deathRecipient = new MyDeathRecipient();
proxy.registerDeathRecippient(deathRecipient, 0); proxy.registerDeathRecipient(deathRecipient, 0);
proxy.unregisterDeathRecipient(deathRecipient, 0); proxy.unregisterDeathRecipient(deathRecipient, 0);
``` ```
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册