mg_broadcast not working

snahmad75
Posts: 445
Joined: Wed Jan 24, 2018 6:32 pm

mg_broadcast not working

Postby snahmad75 » Sun Aug 19, 2018 4:24 pm

Code: Select all

namespace
{
	static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) 
	{

		switch (ev) {
		case MG_EV_POLL:
			break;
		case MG_EV_SEND:
			break;
		case MG_EV_ACCEPT:
			break;
		case MG_EV_CONNECT:
			break;
		case MG_EV_RECV:
			break;
		case MG_EV_CLOSE:
			break;
		default:
			break;
		}
	}

}


void sendUDPMessages()
{
	ACDTRACEF(ES_NoError, "Enter sendUDPMessages \n");
	
	struct mg_mgr mgr;
	mg_mgr_init(&mgr, NULL);

	struct mg_connect_opts opts;

	memset(&opts, 0, sizeof(opts));
	opts.flags = MG_F_ENABLE_BROADCAST;
	opts.iface = nullptr;

	
	const char *port1 = "udp://1457", *port2 = "udp://127.0.0.1:1457";
	const char *port3 = "udp://255.255.255.255:1457";

	struct mg_connection * nc_client = mg_connect_opt(&mgr, port3, ev_handler, opts);	
	
	bool stop = false;
	std::thread thread = std::thread([&]() {
		while (!stop) {
			mg_mgr_poll(&mgr, 1);
		}
	});
	
	for (int i = 1; i <= 10; i++)
	{
		std::string  send_msg = "hello";
		mg_broadcast(&mgr, ev_handler, (void*)send_msg.c_str(), send_msg.size());
	}
	
	std::this_thread::sleep_for(std::chrono::seconds(3));
	
	stop = true;
	if (thread.joinable()) {
		thread.join();
	}

	mg_mgr_free(&mgr);
	
}
Here is my example code.

mg_broadcast not working/ doing nothing on windows platform.
On ESP32 platform it does not build. linker error with mg_broadcast

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: mg_broadcast not working

Postby kolban » Sun Aug 19, 2018 5:18 pm

I had a google on mg_broadcast and esp32 as search terms and there appears to be a number of stories related to that. Have you performed a similar search? Understand that the mg_broadcast capability is part of the Mongoose libraries and is not really related to the Espressif/ESP-IDF code bases. Obviously if Mongoose are using an ESP-IDF function and it isn't working as described, then ESP32 folks will want to hear that ... but for usage of a third party library, it is likely you will want to consult with that third party first.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

snahmad75
Posts: 445
Joined: Wed Jan 24, 2018 6:32 pm

Re: mg_broadcast not working

Postby snahmad75 » Sun Aug 19, 2018 10:11 pm

ok, I will ask on mongoose web server forum.

Who is online

Users browsing this forum: Majestic-12 [Bot] and 92 guests