版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ty497122758/article/details/77387861
物联网Coap协议介绍
Coap(Constrained Application Protocol)是一种特殊的Web协议用在不可靠的设备或者网络,这些设备一般是只有8-bit ROM或RAM的微控制器,这个协议主要用于点到点(machain-to-machain)应用,例如:小电量设备、智能设备,同时支持组播。coap和http类似,我们可以把coap看成http的缩小版,用于小存储不可靠的环境
在各设备间采用request/response的互联模式,有uri的概念(coap://ip:port/path/path/path?p1=v2&p2=v2)
有互联网媒体类型的概念,如下图:
同时也提供:GET、POST、PUT、DELETE四种请求类型
Coap是基于二进制的协议,header只有4个字节,非常紧凑
Californium开源实现
Coap是基于UDP的应用层,但是Californium实现了coap udp和tcp两种传输层协议,单次请求传输限制在1M以内
maven依赖
<dependencies>
<dependency>
<groupId>org.eclipse.californium</groupId>
<artifactId>californium-core</artifactId>
<version>2.0.0-M3</version>
</dependency>
<dependency>
<groupId>org.eclipse.californium</groupId>
<artifactId>element-connector</artifactId>
<version>2.0.0-M3</version>
</dependency>
<dependency>
<groupId>org.eclipse.californium</groupId>
<artifactId>scandium</artifactId>
<version>2.0.0-M3</version>
</dependency>
</dependencies>
服务端示例
package soap;
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
import org.eclipse.californium.core.CoapResource;
import org.eclipse.californium.core.CoapServer;
import org.eclipse.californium.core.network.CoapEndpoint;
import org.eclipse.californium.core.network.config.NetworkConfig;
import org.eclipse.californium.core.server.resources.CoapExchange;
import org.eclipse.californium.elements.tcp.TcpServerConnector;
public class Server {
private static AtomicInteger count = new AtomicInteger();
public static void main(String[] args) {
final CoapServer server = new CoapServer();
//默认就是ucp实现传输层
server.addEndpoint(new CoapEndpoint(new InetSocketAddress("127.0.0.1", 5683)));
//加入tcp实现传输层
server.addEndpoint(new CoapEndpoint(
new TcpServerConnector(new InetSocketAddress("127.0.0.1", 5683), 4, 20000),
NetworkConfig.getStandard()));
//可以加入dtls支持,也就是coaps
// server.addEndpoint(new CoapEndpoint(
// new DTLSConnector(), //这里只是抛砖引玉,需要构建DtlsConnectorConfig
// NetworkConfig.getStandard()));
server.add(new CoapResource("tuyou"){
@Override
public void handleGET(CoapExchange exchange) {
handlePOST(exchange);
};
@Override
public void handlePOST(CoapExchange exchange) { //1
System.out.println(exchange.getRequestOptions().getUriQueryString());
System.out.println(exchange.getRequestText().length());
exchange.respond("asdfasdf");
super.handlePOST(exchange);
}
}.add(new CoapResource("lenovo"){
@Override
public void handlePOST(CoapExchange exchange) { //2
int c = count.incrementAndGet();
if(c % 10000 == 0){
System.out.println(c);
}
exchange.respond(String.valueOf(c));
super.handlePOST(exchange);
}
}));
Executors.newSingleThreadExecutor().execute(() -> {
server.start();
});
}
}
当用户请求url为:coap://localhost:5683/tuyou?p1=v1时,是响应1处的函数
当用户请求url为:coap://localhost:5683/tuyou/lenovo?p1=v1时,是响应2处的函数
客户端示例
package soap;
import java.time.Clock;
import java.util.concurrent.CountDownLatch;
import org.eclipse.californium.core.CoapClient;
import org.eclipse.californium.core.CoapHandler;
import org.eclipse.californium.core.CoapResponse;
import org.eclipse.californium.core.Utils;
import org.eclipse.californium.core.coap.MediaTypeRegistry;
public class Client {
public static void main(String[] args) throws InterruptedException {
int count = 100;
CountDownLatch countDown = new CountDownLatch(count);
long start = Clock.systemUTC().millis();
CoapClient client = new CoapClient("coap://127.0.0.1:5683/tuyou/lenovo?appKey=zq6NDc3sb6QmoQF1&appSecret=PosmJNUoMLD777Nf7tlu");
for(int i = 0; i < count; i++){
client.post(new CoapHandler() {
@Override
public void onLoad(CoapResponse response) {
System.out.println(Utils.prettyPrint(response));
countDown.countDown();
}
@Override
public void onError() {
}
}, "payload message", MediaTypeRegistry.TEXT_PLAIN);
}
countDown.await();
long end = Clock.systemUTC().millis();
System.out.println(end - start);
}
}
提示:示例代码需要用java8运行,否则有些语法会报错
听说,打赏我的人最后都找到了真爱。