Error 1006 abnormal close websocket?

Dear All;
any body have a solution for error 1006 when connect web-socket Client to server after 50 second it disconnect with error 1006
when i make trace for debug i found it disconnect after the wifi connection is refresh
this debug

connected with wifiname, channel 6
dhcp client start...
..ip:192.168.100.130,mask:255.255.255.0,gw:192.168.100.1
Station connected, IP: 192.168.100.130
Connection Opened /* web-socket connected to server*/
Got a Pong! /* when big to server i received PONG /
/
after 50 second */
1006 /web-socket error/
Connnection Closed
ip:192.168.100.130,mask:255.255.255.0,gw:192.168.100.1 /*i see wifi refresh every 50 second auto */

thanks in advance

Error 1006 means there's a low-level issue with websocket itself, or rather the code used to create the websocket. Without posting your code, it's pretty much impossible to help you track down the issue.

My guess, is you aren't creating a new websocket after being closed (when you try to reconnect to the websocket after wifi is refreshed, you're just being sent the close command again)

Once the original connection has been closed, you need to create a new WebSocket object with new event listeners. The problem is vanila websockets attempt to reconnect anyway, so you need a time-out schema to close the socket upon wifi refresh, a wait time so the server can close the socket on it's side, then recreate a new websocket and handlers.

(i could be wrong here tho, it's been eons since i messed with with this stuff)

this is code
#include <ArduinoWebsockets.h>
#include <ESP8266WiFi.h>

const char* ssid = "gggg"; //Enter SSID
const char* password = "123456789"; //Enter Password
const char* websockets_server_host ="10.10.100.120"; //"serverip_or_name"; //Enter server adress

const uint16_t websockets_server_port = 45457;//8080; // Enter server port
WiFiEventHandler gotIpEventHandler, disconnectedEventHandler;

using namespace websockets;
WebsocketsClient client;

String username=String(ESP.getChipId());

void onMessageCallback(WebsocketsMessage message) {
Serial.print("Got Message: ");
Serial.println(message.data());
}

void onEventsCallback(WebsocketsEvent event, String data) {
if(event == WebsocketsEvent::ConnectionOpened) {

    Serial.println("Connnection Opened");
} else if(event == WebsocketsEvent::ConnectionClosed) {
    Serial.println(client.getCloseReason());            
    Serial.println("Connnection Closed");
   // delay(3000);
   // client.connect(websockets_server_host, websockets_server_port, "/ws?username="+username);
} else if(event == WebsocketsEvent::GotPing) {
    Serial.println("Got a Ping!");
} else if(event == WebsocketsEvent::GotPong) {
    Serial.println("Got a Pong!");
}

}

void setup() {
Serial.begin(115200);

  gotIpEventHandler = WiFi.onStationModeGotIP([](const WiFiEventStationModeGotIP& event)

{
Serial.print("Station connected, IP: ");
Serial.println(WiFi.localIP());
});

disconnectedEventHandler = WiFi.onStationModeDisconnected([](const WiFiEventStationModeDisconnected& event)
{
Serial.println("Station disconnected");
});
// Connect to wifi
Serial.setDebugOutput(true);
WiFi.mode(WIFI_AP);
//WiFiClient.setTimeout(3000);

WiFi.begin(ssid, password);


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

//WiFi.mode(WIFI_AP);
// run callback when messages are received
client.onMessage(onMessageCallback);

// run callback when events are occuring
client.onEvent(onEventsCallback);

// Connect to server
//server_host+=username;  


client.connect(websockets_server_host, websockets_server_port, "/ws?username="+username);

// Send a message

// client.send("Hello Server");
Serial.println(WiFi.getMode());
// Send a ping
client.ping();
}

void loop() {
client.poll();
if (Serial.available())
{ String Data=Serial.readString();
Serial.print("Client Send " );
Serial.println(Data);
client.send(Data);
}
}

Can you put that into proper codeblock format?

take this example

change

client.connect(websockets_server_host, websockets_server_port, "/ws?username="+username);

to
client.connectSecure(websockets_server_host, websockets_server_port, "/wss/v2/2/demo/");

and try connect it to this site
https://socketsbay.com/test-websockets

after 50 sec you take Disconnect

Finally i found the solution the problem not from web-socket package its from WIFI when it's DHCP the ESP8266WiFi.h Package is refresh every 50 sec, when the wifi make refresh the websocket close

so i make the wifi static connection
using

IPAddress local_IP(192, 168, 100, 115);

// Set your Gateway IP address
IPAddress gateway(192, 168, 100, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress primaryDNS(8, 8, 8, 8); //optional
IPAddress secondaryDNS(8, 8, 4, 4); //optional

and in setup
if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
Serial.println("STA Failed to configure");
}
WiFi.begin(ssid, password);

this keep the connection stable

i hope this help how have same problem

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.