Quickstart - 508364/NetLeaf GitHub Wiki

快速入门|Quickstart

安装

Windows

build.bat          // 默认x64
build.bat x86      // 32位
build.bat arm64    // ARM64

Linux/WSL

./build.sh         # 默认x64
./build.sh x86     # 32位
./build.sh arm     # ARM
./build.sh arm64   # ARM64

全架构构建

# Linux - 自动检测可用交叉编译工具链
./build_all_linux.sh

# Windows
build_all_windows.bat

第一个示例

#include "netleaf.h"

int main() {
    // 创建Web服务器
    nl_web_server_t* server = nl_web_create(8080);
    
    // 添加页面
    nl_web_add_html(server, "/", "<h1>Hello NetLeaf!</h1>");
    
    // 启动服务器
    nl_web_start(server);
    
    // 保持运行
    while (1) {
#ifdef _WIN32
        Sleep(1000);
#else
        sleep(1);
#endif
    }
    
    return 0;
}

变量替换示例

#include "netleaf.h"

int main() {
    nl_web_server_t* server = nl_web_create(8080);
    
    // 设置编码
    nl_web_set_encoding(server, "UTF-8");
    
    // 定义变量
    const char* vars[] = {"username", "age"};
    const char* values[] = {"张三", "25"};
    
    // 添加带变量的页面
    nl_web_add_html_with_vars(server, "/profile",
        "<h1>欢迎, {{<var>username</var>}}</h1>\n"
        "<p>年龄: {{<var>age</var>}}</p>",
        vars, values, 2);
    
    nl_web_start(server);
    
    while (1) sleep(1);
    return 0;
}

输出结果

用户访问 /profile 时看到的HTML:

<h1>欢迎, 张三</h1>
<p>年龄: 25</p>

注意: 变量在服务器端已被硬编码替换,用户使用F12开发者工具看不到原始变量名。

Vue.js支持

#include "netleaf.h"

int main() {
    nl_web_server_t* server = nl_web_create(8080);
    
    nl_web_add_vue(server, "/app", 
        "<div>\n"
        "  <h1>{{ message }}</h1>\n"
        "  <button @click=\"count++\">点击: {{ count }}</button>\n"
        "</div>");
    
    nl_web_start(server);
    
    while (1) sleep(1);
    return 0;
}

TLS 快速入门

构建 TLS 扩展

# 构建包含 TLS 的版本
mkdir build_with_tls
cd build_with_tls
cmake .. -DBUILD_TLS=ON
cmake --build . --config Release

第一个 TLS 程序

#include "netleaf_tls.h"
#include <stdio.h>

int main() {
    // 初始化 TLS
    nl_tls_init();

    // 创建 TLS 上下文
    nl_tls_ctx_t* ctx = nl_tls_create();

    // 配置 TLS(加载 CA 证书)
    nl_tls_config_t cfg = {0};
    cfg.ca_chain = "ca.crt";
    cfg.verify_peer = 1;
    nl_tls_configure(ctx, &cfg);

    // 连接并握手
    int sock = /* 已建立的 socket */;
    nl_tls_handshake(ctx, sock);

    // 安全通信
    nl_tls_send(ctx, "Hello Secure!", 13);

    // 清理
    nl_tls_close(ctx);
    nl_tls_destroy(ctx);
    nl_tls_shutdown();

    return 0;
}

多语言错误消息

#include "netleaf_tls.h"
#include "netleaf_lang.h"
#include <stdio.h>

int main() {
    nl_tls_init();

    // 设置中文
    nl_lang_set("zh_cn");

    nl_tls_ctx_t* ctx = nl_tls_create();
    nl_tls_config_t cfg = {0};
    cfg.ca_chain = "nonexistent.crt";  // 故意使用错误路径

    int ret = nl_tls_configure(ctx, &cfg);
    if (ret != NL_TLS_OK) {
        // 获取多语言错误消息
        const char* msg = nl_lang_get_error(NL_LIB_TLS, ret);
        printf("错误: %s (原始码: %d)\n", msg, ret);
        // 输出: 错误: CA证书无效或不存在 (原始码: -2)
    }

    nl_tls_destroy(ctx);
    return 0;
}

MQTT 快速入门

构建 MQTT 客户端

# 构建 MQTT(可选含 TLS)
mkdir build_mqtt
cd build_mqtt
cmake .. -DBUILD_MQTT=ON -DBUILD_TLS=ON
cmake --build . --config Release

第一个 MQTT 程序

#include "netleaf_mqtt.h"
#include <stdio.h>

void on_message(void* ud, const nl_mqtt_msg_t* msg) {
    printf("Received: %.*s\n", (int)msg->payload_len, (char*)msg->payload);
}

int main() {
    // 初始化
    nl_mqtt_init();

    // 创建客户端
    nl_mqtt_client_t* client = nl_mqtt_create();

    // 连接
    nl_mqtt_connect_opts_t opts = {0};
    opts.client_id = "quickstart-client";
    nl_mqtt_connect(client, "tcp://broker.hivemq.com:1883", &opts);

    // 订阅主题
    nl_mqtt_subscribe(client, "test/topic", NL_MQTT_QOS_1, NULL);

    // 设置消息回调
    nl_mqtt_set_msg_callback(client, on_message, NULL);

    // 发布消息
    nl_mqtt_publish(client, "test/topic", "Hello MQTT!", 11, NULL);

    // 保持运行
    while (1) {
        nl_mqtt_loop(client, 1000);
    }

    // 清理
    nl_mqtt_disconnect(client);
    nl_mqtt_destroy(client);
    nl_mqtt_shutdown();

    return 0;
}

多语言错误消息

#include "netleaf_mqtt.h"
#include "netleaf_lang.h"
#include <stdio.h>

int main() {
    nl_mqtt_init();

    // 设置中文
    nl_lang_set("zh_cn");

    nl_mqtt_client_t* client = nl_mqtt_create();

    // 尝试连接(故意使用无效地址)
    int ret = nl_mqtt_connect(client, "tcp://invalid:9999", NULL);
    if (ret != 0) {
        // 获取多语言错误消息
        const char* msg = nl_lang_get_error(NL_LIB_MQTT, ret);
        printf("错误: %s (原始码: %d)\n", msg, ret);
        // 输出: 错误: 连接被拒绝 (原始码: -5)
    }

    nl_mqtt_destroy(client);
    return 0;
}
⚠️ **GitHub.com Fallback** ⚠️