Copyright © https://mongoose-os.com

Mongoose OS Forum

frame

WebSocket Secure with ESP32 IDF

Hello all,

I'm using an ESP32 with IDF and I want to develop a websocket client using mongoose. I simply copied the mongoose.c and mongoose.h files to my build tree and after some reading I managed to create a working websocket client that connects to normal websockets (ws).

However, when I want to connect to secure websockets (wss) I can't make it work. I understood that I need to use the mg_connect_ws_opt function with the SSL options, but I still didn't figure out how to enable SSL and how to use configure the options structure.

Can someone help me solve this puzzle?

Regards!

Comments

  • mvpamvpa Lisboa
    edited February 22

    Bellow is the code that I'm using for the ws client. I use the toolchain mentioned in the esp idf documentation to compile and flash.

    #include "freertos/FreeRTOS.h"
    #include "esp_wifi.h"
    #include "esp_system.h"
    #include "esp_event.h"
    #include "esp_event_loop.h"
    #include "nvs_flash.h"
    #include "driver/gpio.h"
    #include "app_wifi.h"
    #include "mongoose.h"
    
    
    static int s_done = 0;
    static int s_is_connected = 0;
    
    static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
      struct websocket_message *wm = (struct websocket_message *) ev_data;
      (void) nc;
    
      char temp[100];
      mg_conn_addr_to_str(nc, temp, sizeof(temp), MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT | MG_SOCK_STRINGIFY_REMOTE);
    
      switch (ev) {
        case MG_EV_CONNECT: {
          int status = *((int *) ev_data);
          if (status != 0) {
            printf("-- Connection error: %d\n", status);
          }
          break;
        }
        case MG_EV_WEBSOCKET_HANDSHAKE_DONE: {
          printf("-- Connected\n");
          s_is_connected = 1;
          break;
        }
        case MG_EV_POLL: {
          if(s_is_connected == 1){
            char *msg = "Hello";
            printf("> %s", msg);
            putchar('\n');
            mg_send_websocket_frame(nc, WEBSOCKET_OP_TEXT, msg, strlen(msg));
          }
          break;
        }
        case MG_EV_WEBSOCKET_CONTROL_FRAME: {
          printf("< %.*s --from control frame\n", (int) wm->size, wm->data);
          break;
        }
        case MG_EV_WEBSOCKET_FRAME: {
          printf("< %.*s --from normal frame\n", (int) wm->size, wm->data);
          break;
        }
        case MG_EV_CLOSE: {
          if (s_is_connected) printf("-- Disconnected\n");
          s_done = 1;
          break;
        }
        default:
            break;
      }
      printf("mongoose event: %d from %s\n", ev, temp);
    }
    
    
    void app_main(void)
    {
        // Wifi part
        esp_err_t ret = nvs_flash_init();
        if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
          ESP_ERROR_CHECK(nvs_flash_erase());
          ret = nvs_flash_init();
        }
        ESP_ERROR_CHECK(ret);
        app_wifi_initialise();
        app_wifi_wait_connected();
    
        // WebSocket part
        struct mg_mgr mgr;
        struct mg_connection *nc;
        const char *chat_server_url = "ws://demos.kaazing.com/echo";
        mg_mgr_init(&mgr, NULL);
    
        nc = mg_connect_ws(&mgr, ev_handler, chat_server_url, "ws_chat", NULL);
        if (nc == NULL) {
            printf("Invalid address\n");
        }
    
        while (!s_done) {
            mg_mgr_poll(&mgr, 1000);
        }
        mg_mgr_free(&mgr);
    }
    
  • mvpamvpa Lisboa

    Thank you!

    I'll check that and post some feedback later.

  • mvpamvpa Lisboa
    edited February 25

    Hello!

    Thank you nliviu for the makefile tips, I think that now I have successfully enabled SSL in my program. However, I can't still seem to find how to use the options structure correctly.

    I tried to do the following, but I couldn't make the connection:

    struct mg_mgr mgr;  
    struct mg_connection *nc;
    struct mg_connect_opts opts;
    static const char *s_ssl_cert = "server.pem";
    static const char *s_ssl_key = "server.key";
    
    memset(&opts, 0, sizeof(opts));
    opts.ssl_cert = s_ssl_cert;
    opts.ssl_key = s_ssl_key;
    mg_mgr_init(&mgr, NULL);
    nc = mg_connect_ws_opt(&mgr, ev_handler, opts, server_url, "ws_echo", NULL);
    

    With this code, the value of nc is NULL.

    I have also the files server.pem and server.key (that I copied from the example) in the same folder as my code.

    Best regards!

Sign In or Register to comment.