简介
Java Californium是一个非常完整的CoAP二方库。
【示例代码仓库】
配置文件
创建一个内存形式配置文件
NetworkConfig config = NetworkConfig.createStandardWithoutFile();
创建一个文件形式配置文件,运行时将在工程根目录下创建一个名为Californium.properties
文件。
NetworkConfig config = NetworkConfig.getStandard();
绑定本机所有网卡指定端口
CoapServer server = new CoapServer();
NetworkConfig config = NetworkConfig.createStandardWithoutFile();
for (InetAddress address : EndpointManager.getEndpointManager().getNetworkInterfaces()) {
InetSocketAddress bindtoAddress = new InetSocketAddress(address, COAP_PORT);
CoapEndpoint.Builder builder = new CoapEndpoint.Builder();
builder.setInetSocketAddress(bindtoAddress);
builder.setNetworkConfig(config);
server.addEndpoint(builder.build());
}
最简单的Server
此处使用coapEndpointBuilder.setInetSocketAddress(new InetSocketAddress(COAP_PORT));
绑定本机所有网卡的5683端口
package org.iotwuxi.embedded;
import org.eclipse.californium.core.CoapResource;
import org.eclipse.californium.core.CoapServer;
import org.eclipse.californium.core.coap.CoAP;
import org.eclipse.californium.core.network.CoapEndpoint;
import org.eclipse.californium.core.network.config.NetworkConfig;
import org.eclipse.californium.core.server.resources.CoapExchange;
import java.net.InetSocketAddress;
/**
* @author xukai
*/
public class HelloServer {
private static final Integer COAP_PORT = NetworkConfig.getStandard().getInt(NetworkConfig.Keys.COAP_PORT);
public static void main(String[] argv) {
// 第一步 配置文件
NetworkConfig networkConfig = NetworkConfig.createStandardWithoutFile();
// 第二步 创建Endpoint
CoapEndpoint.Builder coapEndpointBuilder = new CoapEndpoint.Builder();
coapEndpointBuilder.setNetworkConfig(networkConfig);
coapEndpointBuilder.setInetSocketAddress(new InetSocketAddress(COAP_PORT));
// 第三步 创建CoAP服务器 并绑定Endpoint
CoapServer server = new CoapServer();
server.addEndpoint(coapEndpointBuilder.build());
// 增加CoAP资源的快速方法
server.add(new CoapResource("hello") {
@Override
public void handleGET(CoapExchange exchange) {
exchange.respond(CoAP.ResponseCode.CONTENT, "Hello CoAP!");
}
});
// 增加CoAP资源的常规方法
server.add(new TimeResource());
// 最后,启动服务器
server.start();
}
}
最简单的Client
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.eclipse.californium.core.CoapClient;
import org.eclipse.californium.core.CoapResponse;
import org.eclipse.californium.core.Utils;
import org.eclipse.californium.core.network.CoapEndpoint;
import org.eclipse.californium.core.network.config.NetworkConfig;
import org.eclipse.californium.elements.exception.ConnectorException;
/**
* @author xukai
*/
public class HelloClient {
private static final String serverUrl = "coap://127.0.0.1:5683/hello";
public static void main(String[] args) throws IOException, ConnectorException {
NetworkConfig networkConfig = NetworkConfig.createStandardWithoutFile();
CoapEndpoint.Builder coapEndpointBuilder = new CoapEndpoint.Builder();
coapEndpointBuilder.setNetworkConfig(networkConfig);
CoapClient client = new CoapClient();
client.setEndpoint(coapEndpointBuilder.build());
client.setURI(serverUrl);
CoapResponse response = client.get();
if (response != null) {
System.out.println(response.getCode());
System.out.println(response.getOptions());
System.out.println(response.getResponseText());
System.out.println("\nADVANCED\n");
System.out.println(Utils.prettyPrint(response));
} else {
System.out.println("No response received.");
}
}
}