Esp8266 implementing secure websocket client (unsuccessful, need help)

#1
  1. My goal is to implement secure websocket client to connect to nodejs server wss://www.some_url.com:some_port.

  2. I have try to adapt the websocketchat client example in c/c++.
    Upon receive the acquired IP event from wifi, run the following code

    static struct mg_connection *nc_wss;
    struct mg_connect_opts opts;
    const char *s_ssl_cert = "wss.pem";
    const char *s_ssl_key = "wss.key";
    opts.ssl_cert = s_ssl_cert;
    opts.ssl_key = s_ssl_key;
    nc_wss = mg_connect_ws_opt(mgos_get_mgr(), MG_CB(wss_ev_handler, NULL), opts, "wss://www.some_url.com:some_port", NULL, NULL);
    static void wss_ev_handler(struct mg_connection *nc, int ev,
                                      void *ev_data, void *user_data)
    {
      (void) ev_data;
      (void) nc;
      (void) user_data;

      static unsigned char s_is_connected = 0;
      switch (ev)
      {
        case MG_EV_CONNECT:
        {
          LOG(LL_INFO, ("MG_EV_CONNECT < \n"));
          int status = *((int *) ev_data);
          if (status != 0) {
            LOG(LL_INFO, ("-- Connection error: %d\n", status));
          }
          break;
        }
        case MG_EV_WEBSOCKET_HANDSHAKE_DONE:
        {
          struct http_message *hm = (struct http_message *) ev_data;
          if (hm->resp_code == 101) {
            printf("-- Connected\n");
            s_is_connected = 1;
          } else {
            LOG(LL_INFO, ("-- Connection failed! HTTP code %d\n", hm->resp_code));
            LOG(LL_INFO, ("-- %.*s\n", hm->resp_status_msg.len, hm->resp_status_msg.p));
            LOG(LL_INFO, ("-- %.*s\n", hm->message.len, hm->message.p));
            /* Connection will be closed after this. */
          }
          break;
        }
        case MG_EV_POLL:
        {
          if (s_is_connected == 1)
          {
            const char *msg = "Hello";
            LOG(LL_INFO, ("MG_EV_POLL > %s\n", msg));
            mg_send_websocket_frame(nc, WEBSOCKET_OP_TEXT, msg, strlen(msg));
          }
          break;
        }
        case MG_EV_WEBSOCKET_CONTROL_FRAME:
        {
          struct websocket_message *wm = (struct websocket_message *) ev_data;
          if (wm) LOG(LL_INFO, ("MG_EV_WEBSOCKET_CONTROL_FRAME < %.*s --from control frame\n", (int) wm->size, wm->data));
          break;
        }
        case MG_EV_WEBSOCKET_FRAME:
        {
          struct websocket_message *wm = (struct websocket_message *) ev_data;
          if (wm) LOG(LL_INFO, ("MG_EV_WEBSOCKET_FRAME < %.*s --from normal frame\n", (int) wm->size, wm->data));
          break;
        }
        case MG_EV_CLOSE:
        {
          LOG(LL_INFO, ("-- Disconnected\n"));
          s_is_connected = 0;
          break;
        }
        default:
          break;
      }
    }
  1. The result I see is:
    main.cpp:153            MG_EV_CONNECT < 
    main.cpp:167            -- Connection failed! HTTP code 400
    main.cpp:168            -- Bad Request
    main.cpp:169            -- HTTP/1.1 400 Bad Request
    Connection: close
    main.cpp:201            -- Disconnected

Using simple html with websocket connection over javascript works as nodejs server responds with console.log on the request connection. However, the connection via esp8266 websocket to connect to the same node js server ends with bad request.

Modify the websocket to wss://echo.websocket.org has no error in connection but the connection get disconnected almost instantaneous after getting connect, no handshake message either.

  1. My expectation & question is:
    I would like to have a simple secure websocket client (wss) as in https client to do simple messaging between esp8266 (multiple) and a single nodejs server. At this point, I am lost. I have also use mg_connect_ws instead of mg_connect_ws_opt, the result is the same.