redemption95 / esp-idf

Espressif IoT Development Framework. Official development framework for ESP32.
Apache License 2.0
1 stars 0 forks source link

Websocket Testing #2

Closed redemption95 closed 5 years ago

redemption95 commented 5 years ago

----------------------------- Delete below -----------------------------

If your issue is a general question, starts similar to "How do I..", or is related to 3rd party development kits/libs, please discuss this on our community forum at esp32.com instead.

INSTRUCTIONS

Before submitting a new issue, please follow the checklist and try to find the answer.

If the issue cannot be solved after the steps before, please follow these instructions so we can get the needed information to help you in a quick and effective fashion.

  1. Fill in all the fields under Environment marked with [ ] by picking the correct option for you in each case and deleting the others.
  2. Describe your problem.
  3. Include debug logs on the monitor or the coredump.
  4. Provide more items under Other items if possible can help us better locate your problem.
  5. Use markup (buttons above) and the Preview tab to check what the issue will look like.
  6. Delete these instructions from the above to the below marker lines before submitting this issue.

----------------------------- Delete above -----------------------------

Environment

Problem Description

//Detailed problem description goes here.

Expected Behavior

Actual Behavior

Steps to repropduce

  1. step1
  2. ...

// It helps if you attach a picture of your setup/wiring here.

Code to reproduce this issue

// the code should be wrapped in the ```cpp tag so that it will be displayed better.
#include "esp_log.h"

void app_main()
{

}

// If your code is longer than 30 lines, GIST is preferred.

Debug Logs

Debug log goes here, should contain the backtrace, as well as the reset source if it is a crash.
Please copy the plain text here for us to search the error log. Or attach the complete logs but leave the main part here if the log is *too* long.

Other items if possible

redemption95 commented 5 years ago
static esp_err_t ws_ping_handler(httpd_req_t *req)
{
    uint8_t buf[128] = { 0 };
    httpd_ws_pkt_t ws_pkt;
    memset(&ws_pkt, 0, sizeof(httpd_ws_pkt_t));
    ws_pkt.payload = buf;

    esp_err_t ret = httpd_ws_recv_pkt(req, &ws_pkt, 128, 60000);
    ESP_LOGI(TAG, "Got packet with message: %s", ws_pkt.payload);

    ret = ret ? : httpd_ws_send_pkt(req, &ws_pkt);
    return ret;
}

static const httpd_uri_t ws = {
    .uri        = "/ws",
    .method     = HTTP_GET,
    .handler    = ws_ping_handler,
    .user_ctx   = NULL,
    .is_websocket = true
};

static httpd_handle_t start_webserver(void)
{
    httpd_handle_t server = NULL;
    httpd_config_t config = HTTPD_DEFAULT_CONFIG();

    // Start the httpd server
    ESP_LOGI(TAG, "Starting server on port: '%d'", config.server_port);
    if (httpd_start(&server, &config) == ESP_OK) {
        // Set URI handlers
        ESP_LOGI(TAG, "Registering URI handlers");
        // ...some other HTTP endpoint handlers also add at here
        httpd_register_uri_handler(server, &ws);
        return server;
    }

    ESP_LOGI(TAG, "Error starting server!");
    return NULL;
}