Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
650d6b2a
D
Docs
项目概览
OpenHarmony
/
Docs
1 年多 前同步成功
通知
159
Star
292
Fork
28
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
Docs
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
650d6b2a
编写于
8月 31, 2023
作者:
O
openharmony_ci
提交者:
Gitee
8月 31, 2023
浏览文件
操作
浏览文件
下载
差异文件
!23517 (挑单monthly)ArkTS整改
Merge pull request !23517 from Lixiaoying25/cherry-pick-1693377422
上级
ba4bb5af
a5d7adb5
变更
3
展开全部
隐藏空白更改
内联
并排
Showing
3 changed file
with
1040 addition
and
682 deletion
+1040
-682
zh-cn/application-dev/connectivity/ipc-rpc-development-guideline.md
...ication-dev/connectivity/ipc-rpc-development-guideline.md
+56
-43
zh-cn/application-dev/connectivity/subscribe-remote-state.md
zh-cn/application-dev/connectivity/subscribe-remote-state.md
+26
-16
zh-cn/application-dev/reference/apis/js-apis-rpc.md
zh-cn/application-dev/reference/apis/js-apis-rpc.md
+958
-623
未找到文件。
zh-cn/application-dev/connectivity/ipc-rpc-development-guideline.md
浏览文件 @
650d6b2a
...
...
@@ -180,7 +180,7 @@ IPC/RPC的主要工作是让运行在不同进程的Proxy和Stub互相通信,
1.
添加依赖
```
ts
import
rpc
from
"
@ohos.rpc
"
;
import
rpc
from
'
@ohos.rpc
'
;
// 仅FA模型需要导入@ohos.ability.featureAbility
// import featureAbility from "@ohos.ability.featureAbility";
```
...
...
@@ -188,17 +188,20 @@ IPC/RPC的主要工作是让运行在不同进程的Proxy和Stub互相通信,
Stage模型需要获取context
```
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
,
l
aunchParam
)
{
export
default
class
MainAbility
extends
UI
Ability
{
onCreate
(
want
:
Want
,
launchParam
:
AbilityConstant
.
L
aunchParam
)
{
console
.
log
(
"
[Demo] MainAbility onCreate
"
);
globalThis
.
context
=
this
.
context
;
let
context
=
this
.
context
;
}
onDestroy
()
{
console
.
log
(
"
[Demo] MainAbility onDestroy
"
);
}
onWindowStageCreate
(
windowStage
)
{
onWindowStageCreate
(
windowStage
:
window
.
WindowStage
)
{
// Main window is created, set main page for this ability
console
.
log
(
"
[Demo] MainAbility onWindowStageCreate
"
);
}
...
...
@@ -222,53 +225,62 @@ IPC/RPC的主要工作是让运行在不同进程的Proxy和Stub互相通信,
首先,构造变量want,指定要绑定的Ability所在应用的包名、组件名,如果是跨设备的场景,还需要绑定目标设备NetworkId(组网场景下对应设备的标识符,可以使用deviceManager获取目标设备的NetworkId);然后,构造变量connect,指定绑定成功、绑定失败、断开连接时的回调函数;最后,FA模型使用featureAbility提供的接口绑定Ability,Stage模型通过context获取服务后用提供的接口绑定Ability。
```
ts
import
rpc
from
"
@ohos.rpc
"
;
// 仅FA模型需要导入@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
connectId
=
null
;
let
proxy
:
rpc
.
RemoteProxy
;
let
connectId
:
number
;
// 单个设备绑定Ability
let
want
=
{
let
want
:
Want
=
{
// 包名和组件名写实际的值
"
bundleName
"
:
"
ohos.rpc.test.server
"
,
"
abilityName
"
:
"
ohos.rpc.test.server.ServiceAbility
"
,
bundleName
:
"
ohos.rpc.test.server
"
,
abilityName
:
"
ohos.rpc.test.server.ServiceAbility
"
,
};
let
connect
=
{
onConnect
:
function
(
elementName
,
remote
)
{
let
connect
:
common
.
ConnectOptions
=
{
onConnect
:
(
elementName
,
remote
:
rpc
.
RemoteProxy
)
=>
{
proxy
=
remote
;
},
onDisconnect
:
function
(
elementName
)
{
onDisconnect
:
(
elementName
)
=>
{
},
onFailed
:
function
()
{
proxy
=
null
;
onFailed
:
()
=>
{
proxy
;
}
};
// FA模型使用此方法连接服务
// connectId = featureAbility.connectAbility(want, connect);
connectId
=
globalT
his
.
context
.
connectServiceExtensionAbility
(
want
,
connect
);
connectId
=
t
his
.
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
.
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互相通信,
```
ts
onConnect
(
want
:
Want
)
{
var
robj
:
rpc
.
RemoteObject
=
new
Stub
(
"
rpcTestAbility
"
);
const
robj
:
rpc
.
RemoteObject
=
new
Stub
(
"
rpcTestAbility
"
);
return
robj
;
}
class
Stub
extends
rpc
.
RemoteObject
{
constructor
(
descriptor
)
{
constructor
(
descriptor
:
string
)
{
super
(
descriptor
);
}
onRemoteMessageRequest
(
code
,
data
,
reply
,
option
)
{
onRemoteMessageRequest
(
code
:
number
,
data
:
rpc
.
MessageSequence
,
reply
:
rpc
.
MessageSequence
,
option
:
rpc
.
MessageOption
):
boolean
|
Promise
<
boolean
>
{
// 根据code处理客户端的请求
return
true
;
}
...
...
@@ -297,20 +309,21 @@ IPC/RPC的主要工作是让运行在不同进程的Proxy和Stub互相通信,
客户端在onConnect回调里接收到代理对象,调用sendRequest方法发起请求,在期约(JavaScript期约:用于表示一个异步操作的最终完成或失败及其结果值)或者回调函数里接收结果。
```
ts
import
rpc
from
'
@ohos.rpc
'
;
// 使用期约
let
option
=
new
rpc
.
MessageOption
();
let
data
=
rpc
.
MessageParcel
.
create
();
let
reply
=
rpc
.
MessageParcel
.
create
();
// 往data里写入参数
proxy
.
sendRequest
(
1
,
data
,
reply
,
option
)
.
then
(
function
(
result
)
{
.
then
(
(
result
:
rpc
.
SendRequestResult
)
=>
{
if
(
result
.
errCode
!=
0
)
{
console
.
error
(
"
send request failed, errCode:
"
+
result
.
errCode
);
return
;
}
// 从result.reply里读取结果
})
.
catch
(
function
(
e
)
{
.
catch
(
(
e
:
Error
)
=>
{
console
.
error
(
"
send request got exception:
"
+
e
);
})
.
finally
(()
=>
{
...
...
@@ -319,7 +332,7 @@ IPC/RPC的主要工作是让运行在不同进程的Proxy和Stub互相通信,
})
// 使用回调函数
function
sendRequestCallback
(
result
)
{
function
sendRequestCallback
(
result
:
rpc
.
SendRequestResult
)
{
try
{
if
(
result
.
errCode
!=
0
)
{
console
.
error
(
"
send request failed, errCode:
"
+
result
.
errCode
);
...
...
@@ -352,7 +365,7 @@ IPC/RPC的主要工作是让运行在不同进程的Proxy和Stub互相通信,
// FA模型使用此方法断开连接
// featureAbility.disconnectAbility(connectId, disconnectCallback);
globalT
his
.
context
.
disconnectServiceExtensionAbility
(
connectId
);
t
his
.
context
.
disconnectServiceExtensionAbility
(
connectId
);
```
## 相关实例
...
...
zh-cn/application-dev/connectivity/subscribe-remote-state.md
浏览文件 @
650d6b2a
...
...
@@ -93,17 +93,21 @@ result = object->RemoveDeathRecipient(deathRecipient); // 移除消亡通知
Stage模型在连接服务前需要先获取context
```
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
"
);
globalThis
.
context
=
this
.
context
;
let
context
=
this
.
context
;
}
onDestroy
()
{
console
.
log
(
"
[Demo] MainAbility onDestroy
"
);
}
onWindowStageCreate
(
windowStage
)
{
onWindowStageCreate
(
windowStage
:
window
.
WindowStage
)
{
// Main window is created, set main page for this ability
console
.
log
(
"
[Demo] MainAbility onWindowStageCreate
"
);
}
...
...
@@ -127,36 +131,42 @@ export default class MainAbility extends Ability {
```
ts
// 仅FA模型需要导入@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
connect
=
{
onConnect
:
function
(
elementName
,
remoteProxy
)
{
let
proxy
:
rpc
.
RemoteProxy
;
let
connect
:
common
.
ConnectOptions
=
{
onConnect
:
(
elementName
,
remoteProxy
:
rpc
.
RemoteProxy
)
=>
{
console
.
log
(
"
RpcClient: js onConnect called.
"
);
proxy
=
remoteProxy
;
},
onDisconnect
:
function
(
elementName
)
{
onDisconnect
:
(
elementName
)
=>
{
console
.
log
(
"
RpcClient: onDisconnect
"
);
},
onFailed
:
function
()
{
onFailed
:
()
=>
{
console
.
log
(
"
RpcClient: onFailed
"
);
}
};
let
want
=
{
"
bundleName
"
:
"
com.ohos.server
"
,
"
abilityName
"
:
"
com.ohos.server.EntryAbility
"
,
let
want
:
Want
=
{
bundleName
:
"
com.ohos.server
"
,
abilityName
:
"
com.ohos.server.EntryAbility
"
,
};
// FA模型通过此方法连接服务
// 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
()
{
console
.
log
(
"
server died
"
);
}
}
let
deathRecipient
=
new
MyDeathRecipient
();
proxy
.
registerDeathRecip
p
ient
(
deathRecipient
,
0
);
proxy
.
registerDeathRecipient
(
deathRecipient
,
0
);
proxy
.
unregisterDeathRecipient
(
deathRecipient
,
0
);
```
...
...
zh-cn/application-dev/reference/apis/js-apis-rpc.md
浏览文件 @
650d6b2a
此差异已折叠。
点击以展开。
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录