开发步骤
- 导包。
import { rcp } from '@kit.RemoteCommunicationKit';
import { expect } from '@ohos/hypium';
- 同步写。
const testNetworkInputQueue = async (done: Function): Promise<void> => {
const NetworkInputQueue = rcp.NetworkInputQueue;
const networkInputQueue = new NetworkInputQueue();
let counter = 0;
const interval = setInterval(() => {
networkInputQueue.write('a counter ' + counter++);
if (counter === 10) {
clearInterval(interval);
networkInputQueue.close();
}
}, 100);
const session = rcp.createSession({
requestConfiguration: {
security: {
remoteValidation: 'skip',
},
},
});
const response = await session.post('https://example.org/post', networkInputQueue);
expect(response.statusCode).assertEqual(200);
const obj = response.toJSON();
if (obj) {
expect(obj['data']).assertEqual(
'a counter 0a counter 1a counter 2a counter 3a counter 4a counter 5a counter 6a counter 7a counter 8a counter 9',
);
} else {
expect().assertFail();
}
session.close();
done();
}
- 同步读。
const testNetworkOutputQueue = async (done: Function): Promise<void> => {
const NetworkOutputQueue = rcp.NetworkOutputQueue;
const session = rcp.createSession();
const networkOutputQueue = new NetworkOutputQueue();
const numOfChunks = 10;
const chunkLength = 1000;
const totalBytes = numOfChunks * chunkLength;
const resp = await session.get('https://httpbin.org/bytes/' + totalBytes.toString(), networkOutputQueue);
for (let i = 0; i < numOfChunks; i++) {
const chunk = networkOutputQueue.read(chunkLength);
}
session.close();
done();
}