Your app can send a URPC request, set parameters such as the priority, and receive a URPC response from the remote server. You can cancel a specified or all ongoing URPC requests based on your needs. After the request is completed, you need to close the session to release resources related to URPC.
The capability of calling a remote program using URPC supports phones, 2-in-1 devices, tablets, wearables. Starting from version 5.1.1(19), it is also supported on TVs.
This function requires a remote server. If you have any questions, submit a ticket online.
Modules to Import
import { hilog } from '@kit.PerformanceAnalysisKit';
import { urpc } from '@kit.RemoteCommunicationKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'; Define the class for remote calls, which can be used to define both the input parameter and the return value of the called method. The following is an example:
// Define the input parameter class of the method.
export class MediaTaskRequestMessage {
RequestMessage: urpc.FlowbufElement<string>;
constructor() {
this.RequestMessage = {type: 'STRING', value: '', name: ''};
}
setRequestMessage(RequestMessage: string) {
this.RequestMessage.value = RequestMessage;
}
getRequestMessage(): string {
return this.RequestMessage.value;
}
}
// Define the class for receiving the return value of the called method.
export class MediaTaskResponseMessage {
ResponseMessage: urpc.FlowbufElement<string>;
constructor() {
this.ResponseMessage = {type: 'STRING', value: '', name: ''};
}
setResponseMessage(ResponseMessage: string) {
this.ResponseMessage.value = ResponseMessage;
}
getResponseMessage(): string {
return this.ResponseMessage.value;
}
} Create a request object and a response object.
let request = new MediaTaskRequestMessage(); let response = new MediaTaskResponseMessage();
Configure the connection information and create UrpcStub for initiating URPC calls.
// IP address and port number of the remote server that has been deployed. The following IP address is only an example.
let node: urpc.IpAndPort = {
ip: '192.168.1.1',
port: 8000
}
let connect: urpc.UrpcConnectConfiguration = {
node: node,
protocol: 'eat',
}
let config: urpc.UrpcInitConfiguration = {
timeout: 3000,
mode: 'client',
connect: connect
}
const funcList:string[] = ['uploadFile'];
let urpcStub = urpc.urpcStubCreate(config, funcList); urpcStub.then(async (stub: urpc.UrpcStub) =>{
let upload_config: urpc.CallingOption = {
priority: 0
};
let urpcPromise = stub.call('uploadFile', request, response, upload_config);
urpcPromise.promise.then((resp: object) => {
hilog.info(0x000, 'urpc', 'resp: %{public}s', resp);
}).catch((err: BusinessError) => {
hilog.error(0x000, 'urpc', 'the error code is %d', err.code);
})
}).catch((error: BusinessError) => {
hilog.error(0x000, 'urpc', 'urpc call failed, error code is %d', error.code);
}) After initiating a URPC request with call, you can call cancel to cancel the request with the specified callingId or all ongoing requests initiated using UrpcStub (if callingId is not specified) if you do not need to receive responses.
urpcStub.then(async (stub: urpc.UrpcStub) =>{
let upload_config: urpc.CallingOption = {
priority: 0
};
let urpcPromise = stub.call('uploadFile', request, response, upload_config);
stub.cancel(urpcPromise.callingId);
}).catch((error: BusinessError) => {
hilog.error(0x000, 'urpc', 'urpc cancel failed, error code is %d', error.code);
}) After all URPC requests are processed as required, you can call destroy to release UrpcStub-related resources.
urpcStub.then(async (stub: urpc.UrpcStub) =>{
stub.destroy();
}).catch((error: BusinessError) => {
hilog.error(0x000, 'urpc', 'urpc destroy failed, error code is %d', error.code);
})