我正在尝试使用 ESPAsycnWebserver lib 使用 websocket 构建异步网络服务器。一切正常,但有一个时刻:当我多次刷新时:13 或 29 它会崩溃并重新启动。我认为有内存问题,但不知道如何解决。这是控制台输出和工作代码:
Guru Meditation Error: Core 1 panic'ed (LoadProhibited). Exception was unhandled.
Core 1 register dump:
PC : 0x401705a7 PS : 0x00060c30 A0 : 0x800d30de A1 : 0x3ffb1ef0
A2 : 0x3ffb1f3c A3 : 0x00000001 A4 : 0x80089a44 A5 : 0x3ffd0ec0
A6 : 0x00000000 A7 : 0x3ffb0060 A8 : 0x3ffc6e17 A9 : 0x00000001
A10 : 0x00000003 A11 : 0x00060a23 A12 : 0x00000005 A13 : 0x00000020
A14 : 0x00000020 A15 : 0x3ffd1391 SAR : 0x00000018 EXCCAUSE: 0x0000001c
EXCVADDR: 0x00000001 LBEG : 0x4000c2e0 LEND : 0x4000c2f6 LCOUNT : 0xffffffff
ELF file SHA256: 0000000000000000
Backtrace: 0x401705a7:0x3ffb1ef0 0x400d30db:0x3ffb1f10 0x400d31e9:0x3ffb1f30 0x400d3231:0x3ffb1f70 0x400d0ed4:0x3ffb1f90 0x400dffdd:0x3ffb1fb0 0x40089832:0x3ffb1fd0
#0 0x401705a7:0x3ffb1ef0 in std::_Function_handler<bool (AsyncWebSocketClient* const&), AsyncWebSocket::count() const::{lambda(AsyncWebSocketClient*)#1}>::_M_invoke(std::_Any_data const&, AsyncWebSocketClient* const&) at .pio\libdeps\esp32doit-devkit-v1\ESP Async WebServer\src/AsyncWebSocket.cpp:850
#1 0x400d30db:0x3ffb1f10 in std::function<bool (AsyncWebSocketClient* const&)>::operator()(AsyncWebSocketClient* const&) const at .pio\libdeps\esp32doit-devkit-v1\ESP Async WebServer\src/AsyncWebSocket.cpp:850
#2 0x400d31e9:0x3ffb1f30 in LinkedList<AsyncWebSocketClient*, LinkedListNode>::count_if(std::function<bool (AsyncWebSocketClient* const&)>) const at .pio\libdeps\esp32doit-devkit-v1\ESP Async WebServer\src/AsyncWebSocket.cpp:850
(inlined by) AsyncWebSocket::count() const at .pio\libdeps\esp32doit-devkit-v1\ESP Async WebServer\src/AsyncWebSocket.cpp:904
#3 0x400d3231:0x3ffb1f70 in AsyncWebSocket::cleanupClients(unsigned short) at .pio\libdeps\esp32doit-devkit-v1\ESP Async WebServer\src/AsyncWebSocket.cpp:850
#4 0x400d0ed4:0x3ffb1f90 in loop() at src/main.cpp:82
#5 0x400dffdd:0x3ffb1fb0 in loopTask(void*) at C:\Users\_Terminal\.platformio\packages\framework-arduinoespressif32\cores\esp32/main.cpp:23
#6 0x40089832:0x3ffb1fd0 in vPortTaskWrapper at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/port.c:355 (discriminator 1)
Rebooting...
ets Jun 8 2016 00:22:57
rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:1044
load:0x40078000,len:10124
load:0x40080400,len:5828
entry 0x400806a8
代号:正面
<script>
var gateway = `ws://192.168.0.105/ws`;
var websocket;
window.addEventListener('load', onLoad);
function initWebSocket() {
console.log('Trying to open a WebSocket connection...');
websocket = new WebSocket(gateway);
websocket.onopen = onOpen;
websocket.onclose = onClose;
websocket.onmessage = onMessage; // <-- add this line
}
function onOpen(event) {
console.log('Connection opened');
}
function onClose(event) {
console.log('Connection closed');
setTimeout(initWebSocket, 2000);
}
function onMessage(event) {
var state;
if (event.data == "1"){
state = "ON";
}
else{
state = "OFF";
}
document.getElementById('state').innerHTML = state;
}
function onLoad(event) {
initWebSocket();
initButton();
}
function initButton() {
document.getElementById('button').addEventListener('click', toggle);
}
function toggle(){
websocket.send('toggle');
}
</script>
代码:ESP
// Import required libraries
#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
// Replace with your network credentials
const char* ssid = "home";
const char* password = "qwertyui";
bool ledState = 0;
const int ledPin = 26;
// Create AsyncWebServer object on port 80
AsyncWebServer server(80);
AsyncWebSocket ws("/ws");
void onEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len) {
if(type == WS_EVT_CONNECT){
Serial.printf("New Client #%u from %s\n",client->id(),client->remoteIP().toString().c_str());
client->ping();
}
else if(type == WS_EVT_DISCONNECT){
Serial.printf("ws[%s][%u] disconnect: %u\n", server->url(), client->id(), client->id());
}
else if(type == WS_EVT_ERROR){
Serial.printf("ws[%s][%u] error(%u): %s\n", server->url(), client->id(), *((uint16_t*)arg), (char*)data);
}
else if(type == WS_EVT_PONG){
Serial.printf("ws[%s][%u] pong[%u]: %s\n", server->url(), client->id(), len, (len)?(char*)data:"");
}
else if(type == WS_EVT_DATA){
AwsFrameInfo* info = (AwsFrameInfo*)arg;
if(info->final && info->index==0 && info->len == len){
Serial.printf("ws[%s][%u] %s-message[%llu]: ", server->url(), client->id(), (info->opcode == WS_TEXT)?"text":"binary", info->len);
if(info->opcode == WS_TEXT){
data[len] = 0;
Serial.printf("%s\n", (char*)data);
if(strcmp((char*)data,"toggle")==0){
ledState = !ledState;
digitalWrite(ledPin,ledState);
}
}
}
}
}
void setup(){
// Serial port for debugging purposes
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi..");
}
// Print ESP Local IP Address
Serial.println(WiFi.localIP());
ws.onEvent(onEvent);
server.addHandler(&ws);
// Start server
server.begin();
}
void loop() {
ws.cleanupClients();
}