mirror of
https://github.com/cesanta/mongoose.git
synced 2026-10-28 12:30:58 +00:00
update to new API
This commit is contained in:
1 parent
199af33b30
commit
ed54b60ea3
11 files changed
+73
-54
No files matched your search
@@ -9,27 +9,21 @@
|
||||
//
|
||||
// To enable SSL/TLS, see https://mongoose.ws/tutorials/tls/#how-to-build
|
||||
|
||||
#include "mongoose.h"
|
||||
|
||||
static const char *s_url =
|
||||
#if defined(MG_ENABLE_MBEDTLS) || defined(MG_ENABLE_OPENSSL)
|
||||
#if MG_TLS
|
||||
"wss://broker.hivemq.com:8884/mqtt";
|
||||
#else
|
||||
"ws://broker.hivemq.com:8000/mqtt";
|
||||
#endif
|
||||
|
||||
#include "mongoose.h"
|
||||
|
||||
static const char *s_topic = "mg/test";
|
||||
|
||||
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
if (ev == MG_EV_ERROR) {
|
||||
// On error, log error message
|
||||
MG_ERROR(("%p %s", c->fd, (char *) ev_data));
|
||||
} else if (ev == MG_EV_CONNECT) {
|
||||
// If target URL is SSL/TLS, command client connection to use TLS
|
||||
if (mg_url_is_ssl(s_url)) {
|
||||
struct mg_tls_opts opts = {.ca = "ca.pem"};
|
||||
mg_tls_init(c, &opts);
|
||||
}
|
||||
} else if (ev == MG_EV_WS_OPEN) {
|
||||
// WS connection established. Perform MQTT login
|
||||
MG_INFO(("Connected to WS. Logging in to MQTT..."));
|
||||
@@ -98,6 +92,8 @@ int main(void) {
|
||||
struct mg_mgr mgr; // Event manager
|
||||
bool done = false; // Event handler flips it to true when done
|
||||
mg_mgr_init(&mgr); // Initialise event manager
|
||||
struct mg_tls_opts opts = {.client_ca = mg_str(CA_ALL)};
|
||||
mg_tls_ctx_init(&mgr, &opts);
|
||||
mg_log_set(MG_LL_DEBUG); // Set log level
|
||||
mg_ws_connect(&mgr, s_url, fn, &done, NULL); // Create client connection
|
||||
while (done == false) mg_mgr_poll(&mgr, 1000); // Event loop
|
||||
|
||||
Reference in new issue
Block a user