OpenHarmony三方库:ohos_coap

介绍

libcoap是Coap协议的C语言实现,它是使用Coap协议的工具。 ohos_coap是基于libcoap v4.3.1版本,封装napi接口,给上层ts提供coap通信能力的三方库。

目前客户端实现的能力:

  • 支持coap客户端发起get,post,put请求

目前服务端实现的能力:

  • 支持coap服务端处理get,post数据

暂不支持的能力:

  • coap服务端除注册get、post请求外其余功能不支持
  • coap客户端发送文件,媒体软件能力
  • 不支持tls/dtls的消息
  • 不支持发起 delete请求

使用本工程

有两种方式可以下载本工程:

  1. 开发者如果想要使用本工程,可以使用git命令
git clone https://gitee.com/openharmony-tpc/ohos_coap.git --recurse-submodules
  1. 点击下载按钮,把本工程下到本地,再把libcoap v4.3.1版本代码放入libcoap/src/cpp/libcoapSource目录下,这样才可以编译通过。

  2. 漏洞修复,进入到libcoap\src\main\cpp\thirdModule 目录下,执行 modify.sh 脚本,将本目录下的 patch 文件合入到 libcoap 源码中。 

下载安装

ohpm install @ohos/coap

OpenHarmony ohpm 环境配置等更多内容,请参考如何安装 OpenHarmony ohpm 包

X86模拟器配置

使用模拟器运行应用/服务

使用

1、coap get 请求展示

// 打开native log开关
CoapClient.setNativeLogOpen(true);
// 发起coap get请求,注意this.coapUri请使用者换成真实的url
// 每一个CoapClient对应一个COAP请求任务,不可复用
let coapClient = new CoapClient();
let coapGet = coapClient.request(this.coapUri, CoapRequestMethod.GET, CoapRequestType.COAP_MESSAGE_CON);
console.log("libcoap get test");
//coap get请求异步回调
coapGet.then((data) => {
  if (data.code == CoapResponseCode.SUCCESS) { //sucess说明请求成功 
    console.log("libcoap get:" + data.message[0]);
  } else {
    console.log("libcoap get code:" + data.code);
    console.log("libcoap get error message:" + data.message);
  }
})

let coapClient = new CoapClient();
get请求CaLLBACK回调
coapClient.requestCall(this.coapUri, CoapRequestMethod.GET, CoapRequestType.COAP_MESSAGE_CON, (data: CoapResponse) => {
  if (data.code == CoapResponseCode.SUCCESS) {
    console.log("libcoap requestCall get:" + data.message[0]);
  } else {
    console.log("libcoap requestCall get code:" + data.code);
    console.log("libcoap requestCall get error message:" + data.message[0]);
  }
});

2、coap put 请求展示

// 打开native log开关
CoapClient.setNativeLogOpen(true);
let coapClient = new CoapClient();
//设置端口号
coapClient.setPort(5683);
//设置透传数据的格式
coapClient.setFormat(ContentFormat.PLAIN);
//设置透传数据
coapClient.setPayload("this is put test payload");
// 发起coap put请求,注意this.coapUri请使用者换成真实的url
// 每一个CoapClient对应一个COAP请求任务,不可复用
let coapPut = coapClient.request(this.coapUri, CoapRequestMethod.PUT, CoapRequestType.COAP_MESSAGE_CON);
console.log("libcoap put test");
//coap get请求异步回调
coapPut.then((data) => {
  if (data.code == CoapResponseCode.SUCCESS) { //sucess说明请求成功 
    console.log("libcoap put:" + data.message[0]);
  } else {
    console.log("libcoap put code:" + data.code);
    console.log("libcoap put error message:" + data.message);
  }
})

服务端使用

服务器注册get,post请求

let server = new CoapServer();
let resource = new CoapResource();
let exchange = new CoapExchange();
//服务器本地IP,端口号目前固定
server.setAddr('125.0.10.5', '0');
// 打开native log开关
CoapClient.setNativeLogOpen(true);
//添加id
let resourceId = this.resource.createResource('test');
//添加资源属性
resource.addAttributes(resourceId, 'title', 'Dynamic');
//处理get 数据
resource.registerGetHandler(resourceId, (responseId: string) => {
 let getQuesOptions = exchange.getRequestOptions(responseId);
 exchange.response(responseId, CoapResourceCode.COAP_RESPONSE_CODE_CONTENT, 'hello arkts');
});
//处理post 数据
resource.registerPostHandler(resourceId, (responseId: string) => {
let postQuesOptions = exchange.getRequestOptions(responseId);
let postQuesText = exchange.getRequestText(responseId); 
 exchange.response(responseId, CoapResourceCode.COAP_RESPONSE_CODE_CONTENT, 'hello arkts');
});
//添加资源
server.addResource(resourceId);
//coap server 启动服务器
server.start();

客户端接口说明

方法名入参接口描述
request(coapUri: string, method: CoapRequestMethod, type: CoapRequestType)coapUri: string, method: CoapRequestMethod, type: CoapRequestTyp客户端发起请求
requestCall(coapUri: string, method: CoapRequestMethod, type: CoapRequestType, callback: AsyncCallback)coapUri: string, method: CoapRequestMethod, type: CoapRequestTyp, callback: AsyncCallback客户端发起请求
setFormat(format: ContentFormat)format: ContentFormat客服端发起post,put请求时透传参数的格式
setPort(port: number)port: number设置端口号
setWaitSecond(waitSecond: number)waitSecond: number客户端设置请求等待超时时间
setObsSecond(obsSecond: number)obsSecond: number客户端设置连接持续观察等待时间
setRepeatCount(repeatCount: number)repeatCount: number客户端设置重试请求次数
setPayload(payload: string)payload: string设置客服端透传的内容
setToken(token: Uint8Array)token: Uint8Array设置token
setNativeLogOpen(isOpen: boolean)isOpen: boolean是否打开native侧的log
addOption(optionNum: number, value: string)optionNum: number, value: string客户端自定义新增一个option选项
setMid(mid: number)mid: number客户端自定义报文的message id
setPayloadBinary(payload: Uint8Array)payload: Uint8Array客户端透传的字节内容
setBlockMode(mode: CoapBlockMode)mode: CoapBlockMode客户端的块传输模式

单元测试用例详情见TEST.md

server端接口说明

方法名入参接口描述
start(classId: number)classId: number启动服务器
stop(classId: number)classId: number关闭服务器
addResource(classId: number, resourceID: string)resourceID: string添加资源
setAddr(classId: number, ip: string, port: string)ip: string, port: string设置服务IP和端口(0表示默认端口)
getStatus(classId: number)classId: number判断是否启动状态码
registerGetHandler(resourceID: string, cb: Function)resourceID: string, cb: Function添加get请求处理器
registerPostHandler(resourceID: string, cb: Function)resourceID: string, cb: Function添加post请求处理器
addAttributes: (resourceId: string, attributesOptionOne: string, attributesOptionTwo: string)(resourceId: string, attributesOptionOne: string, attributesOptionTwo: string)添加资源属性
createResource: (resourceId: string)resourceId: string添加Id
destroy()注销资源
response: (responseId: string, code: number, data: string)(responseId: string, code: number, data: string)发送响应到客户端
getRequestOptions: (responseId: string)responseId: string获取请求选项
getRequestText: (responseId: string)responseId: string获取请求负载
getSourceAddress: (responseId: string)responseId: string对端IP

约束与限制

在下述版本验证通过:

DevEco Studio: NEXT Developer Beta1-5.0.3.331, SDK: API12(5.0.0.25(SP6))

目录结构

|----ohos_coap
|     |---- entry  # 示例代码文件夹
|     |---- libcoap  # ohos_coap库文件夹
|           |---- index.ets  # 对外接口
|     |---- README_CN.md  # 使用说明文档

最后

有很多小伙伴不知道学习哪些鸿蒙开发技术?不知道需要重点掌握哪些鸿蒙应用开发知识点?而且学习时频繁踩坑,最终浪费大量时间。所以有一份实用的鸿蒙(HarmonyOS NEXT)资料用来跟着学习是非常有必要的。 

这份鸿蒙(HarmonyOS NEXT)资料包含了鸿蒙开发必掌握的核心知识要点,内容包含了ArkTS、ArkUI开发组件、Stage模型、多端部署、分布式应用开发、音频、视频、WebGL、OpenHarmony多媒体技术、Napi组件、OpenHarmony内核、OpenHarmony南向开发、鸿蒙项目实战等等)鸿蒙(HarmonyOS NEXT)技术知识点。

希望这一份鸿蒙学习资料能够给大家带来帮助,有需要的小伙伴自行领取,限时开源,先到先得~无套路领取!!

获取这份完整版高清学习路线,请点击→纯血版全套鸿蒙HarmonyOS NEXT学习资料

鸿蒙(HarmonyOS NEXT)最新学习路线

  •  HarmonOS基础技能

  • HarmonOS就业必备技能 
  •  HarmonOS多媒体技术

  • 鸿蒙NaPi组件进阶

  • HarmonOS高级技能

  • 初识HarmonOS内核 
  • 实战就业级设备开发

有了路线图,怎么能没有学习资料呢,小编也准备了一份联合鸿蒙官方发布笔记整理收纳的一套系统性的鸿蒙(OpenHarmony )学习手册(共计1236页)鸿蒙(OpenHarmony )开发入门教学视频,内容包含:ArkTS、ArkUI、Web开发、应用模型、资源分类…等知识点。

获取以上完整版高清学习路线,请点击→纯血版全套鸿蒙HarmonyOS NEXT学习资料

《鸿蒙 (OpenHarmony)开发入门教学视频》

《鸿蒙生态应用开发V2.0白皮书》

图片

《鸿蒙 (OpenHarmony)开发基础到实战手册》

OpenHarmony北向、南向开发环境搭建

图片

 《鸿蒙开发基础》

  • ArkTS语言
  • 安装DevEco Studio
  • 运用你的第一个ArkTS应用
  • ArkUI声明式UI开发
  • .……

图片

 《鸿蒙开发进阶》

  • Stage模型入门
  • 网络管理
  • 数据管理
  • 电话服务
  • 分布式应用开发
  • 通知与窗口管理
  • 多媒体技术
  • 安全技能
  • 任务管理
  • WebGL
  • 国际化开发
  • 应用测试
  • DFX面向未来设计
  • 鸿蒙系统移植和裁剪定制
  • ……

图片

《鸿蒙进阶实战》

  • ArkTS实践
  • UIAbility应用
  • 网络案例
  • ……

图片

 获取以上完整鸿蒙HarmonyOS学习资料,请点击→纯血版全套鸿蒙HarmonyOS NEXT学习资料

总结

总的来说,华为鸿蒙不再兼容安卓,对中年程序员来说是一个挑战,也是一个机会。只有积极应对变化,不断学习和提升自己,他们才能在这个变革的时代中立于不败之地。 

  • 10
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值