Java Californium 使用總結(jié)

簡介

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.");
        }
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容