ArduinoWebsockets icon indicating copy to clipboard operation
ArduinoWebsockets copied to clipboard

ESP32-Server Not Working Properly receives one msg and gets disconnected

Open ashutosh7777 opened this issue 3 years ago • 4 comments

/**************************************************************************************************************************** image

ESP32-Server.ino For ESP32.

/* Minimal Esp32 Websockets Server

This sketch: 1. Connects to a WiFi network 2. Starts a websocket server on port 80 3. Waits for connections 4. Once a client connects, it wait for a message from the client 5. Sends an "echo" message to the client 6. closes the connection and goes back to step 3

Hardware: For this sketch you only need an ESP32 board.

Created 15/02/2019 By Gil Maimon https://github.com/gilmaimon/ArduinoWebsockets */

#include <ArduinoWebsockets.h> #include <WiFi.h>

const char* ssid = "only"; //Enter SSID const char* password = "ashu1234"; //Enter Password

using namespace websockets;

WebsocketsServer server; void setup() { Serial.begin(115200); // Connect to wifi WiFi.begin(ssid, password);

// Wait some time to connect to wifi for(int i = 0; i < 15 && WiFi.status() != WL_CONNECTED; i++) { Serial.print("."); delay(1000); }

Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); //You can get IP address assigned to ESP

server.listen(80); Serial.print("Is server live? "); Serial.println(server.available()); }

void loop() { WebsocketsClient client = server.accept(); if(client.available()) { WebsocketsMessage msg = client.readBlocking();

// log
Serial.print("Got Message: ");
Serial.println(msg.data());

// return echo

// client.send("Echo: " + msg.data());

// close the connection

// client.close(); }

delay(1000); } 161898290-8091c2db-0d2f-423b-bfe1-b2fda9996ee7

any help will be great for us. thank you in advance

ashutosh7777 avatar Apr 08 '22 06:04 ashutosh7777

When the client object goes out of scope, the connection is closed. If you want to keep receiving/sending messages, you should not let the object go out of scope. Instead keep interacting with it until you want it closed. You could check the wiki for more information and advanced examples for handling multiple clients and so on.

gilmaimon avatar Apr 08 '22 10:04 gilmaimon

ya i tried advance client also issue remains same

ashutosh7777 avatar Apr 13 '22 08:04 ashutosh7777

same problem here. can't find any solution. @ashutosh7777 did you find any solution?

AsadDUET avatar Aug 11 '23 15:08 AsadDUET