簡介
Java Californium是一個非常完整的CoAP二方庫。
【示例代碼倉庫】
配置文件
創(chuàng)建一個內(nèi)存形式配置文件
NetworkConfig config = NetworkConfig.createStandardWithoutFile();
創(chuàng)建一個文件形式配置文件,運(yùn)行時將在工程根目錄下創(chuàng)建一個名為Californium.properties文件。
NetworkConfig config = NetworkConfig.getStandard();
綁定本機(jī)所有網(wǎng)卡指定端口
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));綁定本機(jī)所有網(wǎng)卡的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();
// 第二步 創(chuàng)建Endpoint
CoapEndpoint.Builder coapEndpointBuilder = new CoapEndpoint.Builder();
coapEndpointBuilder.setNetworkConfig(networkConfig);
coapEndpointBuilder.setInetSocketAddress(new InetSocketAddress(COAP_PORT));
// 第三步 創(chuàng)建CoAP服務(wù)器 并綁定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資源的常規(guī)方法
server.add(new TimeResource());
// 最后,啟動服務(wù)器
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.");
}
}
}