-
Notifications
You must be signed in to change notification settings - Fork 576
Description
I tried many times to connect to the websocket server but failed。I used wireShark to capture packets but did not find the corresponding ip data packet。
The source code is as follows:
/*
* websocket_test.ino
*
* Created on: 08.06.2021
*
*/
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <WebSocketsClient.h>
ESP8266WiFiMulti WiFiMulti;
WebSocketsClient webSocket;
void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
switch(type) {
case WStype_DISCONNECTED:
Serial.printf("[WSc] Disconnected!\n");
webSocket.sendTXT("DisConnected");
break;
case WStype_CONNECTED: {
Serial.printf("[WSc] Connected to url: %s\n", payload);
// send message to server when Connected
webSocket.sendTXT("Connected");
}
break;
case WStype_TEXT:
Serial.printf("[WSc] get text: %s\n", payload);
// send message to server
// webSocket.sendTXT("message here");
break;
case WStype_BIN:
Serial.printf("[WSc] get binary length: %u\n", length);
hexdump(payload, length);
// send data to server
// webSocket.sendBIN(payload, length);
break;
case WStype_PING:
// pong will be send automatically
Serial.printf("[WSc] get ping\n");
break;
case WStype_PONG:
// answer to a ping we send
Serial.printf("[WSc] get pong\n");
break;
}
}
void setup() {
Serial.begin(115200);
WiFi.begin("vivo X21", "12345678");
Serial.println();
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("Connected, IP address: ");
Serial.println(WiFi.localIP());
// server address, port and URL
webSocket.begin("192.168.1.104", 5000);
// event handler
webSocket.onEvent(webSocketEvent);
// use HTTP Basic Authorization this is optional remove if not needed
// webSocket.setAuthorization("user", "Password");
// try ever 5000 again if connection has failed
webSocket.setReconnectInterval(5000);
// start heartbeat (optional)
// ping server every 15000 ms
// expect pong from server within 3000 ms
// consider connection disconnected if pong is not received 2 times
webSocket.enableHeartbeat(15000, 3000, 2);
}
void loop() {
webSocket.loop();
}
The information on Serial is as follows:
Connecting.....scandone
state: 0 -> 2 (b0)
.state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 1
cnt
connected with vivo X21, channel 1
dhcp client start...
....ip:192.168.1.107,mask:255.255.255.0,gw:192.168.1.1
.
Connected, IP address: 192.168.1.107
[WSc] Disconnected!
[WSc] Disconnected!
[WSc] Disconnected!
[WSc] Disconnected!
[WSc] Disconnected!
[WSc] Disconnected!
[WSc] Disconnected!
[WSc] Disconnected!
[WSc] Disconnected!
[WSc] Disconnected!
[WSc] Disconnected!
[WSc] Disconnected!
[WSc] Disconnected!
[WSc] Disconnected!
[WSc] Disconnected!
[WSc] Disconnected!
[WSc] Disconnected!
[WSc] Disconnected!
[WSc] Disconnected!
[WSc] Disconnected!
[WSc] Disconnected!
[WSc] Disconnected!
[WSc] Disconnected!
[WSc] Disconnected!
[WSc] Disconnected!
[WSc] Disconnected!