Need help getting WebSocket Server and Http Web Server to use different ports

Ravenholem
Posts: 19
Joined: Wed Nov 10, 2021 7:13 pm

Need help getting WebSocket Server and Http Web Server to use different ports

Postby Ravenholem » Wed Nov 10, 2021 7:46 pm

Hello to all!
This is my first post on the forums as we have just migrated over from AVR to the ESP32 platform as our new base for future projects.

I have been knee deep in the ESP32 water the past few days trying to get Wifi Soft AP web interface up and running use WebSocket

The thing I am struggling with is how to get the websocket server to use a different PORT than the Http Webserver.
Currently when I connect to the ESP32 I can go to "http://192.168.4.1" and it will open up my webpage and in that page I have scrip that connects to the websocket server at "ws://192.168.4.1/ws" My problem is I don't want to share PORT 80. I am trying to make it so that when the script on the webpage loads it connects to a websocket server that is setup on "ws://192.168.4.1:3030"

The esp32 is running as a wifi AP so I'm not worried about conflicting with other stuff on a network. The main goal is to make a Wifi AP that our users can connect to and see data or enter data using websocket. From my understanding it is better to have the websocket server on its own port so that way all communication is directly to it and not passing everything through the WebServer.

I used the example Websocket Echo and started to build out from there but then found this library/example https://github.com/Molorius/esp32-websocket which was dead on what we are trying to figure out how to add the ability to create the websocket server on its own port separate of the Webserver part. Then I was going to try and submit a update for it.

This current project I'm working on is sorta working in Arduino which was easy because the websocket can just be created with the port you want. So all of my pre existing HTML and Java is setup to work with it. So if I can get this part figured out with getting the websocket server listening on a different port I can just set the port and my existing HTML/Java should work.

Thank you anyone that helps, Hopefully someone has a idea. This is the part I'm stuck on up till now Things were moving rather easily with the IDF examples at hand... ^_^
Last edited by Ravenholem on Thu Nov 11, 2021 2:27 pm, edited 1 time in total.

ESP_Sprite
Posts: 9619
Joined: Thu Nov 26, 2015 4:08 am

Re: Need help getting WebSocket Server and Http Web Server to use different ports

Postby ESP_Sprite » Thu Nov 11, 2021 2:21 am

Ravenholem wrote:
Wed Nov 10, 2021 7:46 pm
From my understanding it is better to have the websocket server on its own port so that way all communication is directly to it and not passing everything through the WebServer.
That may be true on a traditional webserver, but for the webserver in esp-idf, the two are integrated. If you want to run something on a different port, it would mean either having the webserver listen on a 2nd port, or instantiating a 2nd webserver alltogether, both of which are less optimal than simply running one webserver on one port (and I'm not sure if the esp-httpd infrastructure easily supports either of these options). If this is the leading consideration for wanting a separate port, I'd say it's better to drop that requirement.

Ravenholem
Posts: 19
Joined: Wed Nov 10, 2021 7:13 pm

Re: Need help getting WebSocket Server and Http Web Server to use different ports

Postby Ravenholem » Thu Nov 11, 2021 2:23 pm

Thank you for the reply.
In the time between my post and now. I figured out how to create two servers that listen on two ports one on 80 with URI for handling pages and the other on 1337 handling WS requests and traffic.
Though as you stated, I feel like that is very inefficient. I could not figure out how to get the socket to listen to two ports at the same time.

In essence this is what I have. In Main() I call these two on startup. The websocket server just takes "/" uri and handles it and 404's everything else. In my understanding I'm running two instances of HTTPD at this point correct? Which is probably not a great idea. Although I have found some mentions of how to handle this problem and it seems like having two sockets open is the only solution one for websocket and one for http. Unless they share the same socket.

As you said in the closing of your reply, you may be right and I should just give up on different port's. I'm guessing the HTTPD websocket server is handling everything either way. I'm not having to scan the header to look for the socket upgrade or anything. I just need to gain access to the data coming over the socket ie "WEBSOCKET_TEXT or BIN" and do what I need done.
I just can't tell if the HTTPD is handling the disconnect of Websocket connections or not.

Code: Select all

// Http Server
static httpd_handle_t start_Http_server(void)
{
    static const char *TAG = ": Http Server :";
    httpd_handle_t Http_server = NULL;
    httpd_config_t Http_config = HTTPD_DEFAULT_CONFIG();
    Http_config.lru_purge_enable = true;

    // Start the httpd server
    ESP_LOGI(TAG, "Starting server on port: '%d'", Http_config.server_port);
    if (httpd_start(&Http_server, &Http_config) == ESP_OK) {
        // Set URI handlers
        ESP_LOGI(TAG, "Registering URI handlers");
        httpd_register_uri_handler(Http_server, &hello);
        //httpd_register_uri_handler(server, &echo);
        //httpd_register_uri_handler(server, &ctrl);
        #if CONFIG_EXAMPLE_BASIC_AUTH
        httpd_register_basic_auth(Http_server);
        #endif
        return Http_server;
    }

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


// Web Socket Server
static httpd_handle_t start_WebSocket_server(void)
{
    static const char *TAG = ": WebSocket_Server :";
    httpd_handle_t WebSocket_server = NULL;
    httpd_config_t config = HTTPD_DEFAULT_CONFIG();
    config.server_port = 1337;
    config.ctrl_port = 42768;
    // Start the httpd server
    ESP_LOGI(TAG, "Starting Web Socket Server on port: '%d'", config.server_port);
    if (httpd_start(&WebSocket_server, &config) == ESP_OK) {
        // Registering the ws handler
        ESP_LOGI(TAG, "Registering URI handlers");
        httpd_register_uri_handler(WebSocket_server, &ws);
        return WebSocket_server;
    }

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

Who is online

Users browsing this forum: Bing [Bot] and 86 guests