Files
xiaozhi-esp32/main/boards/otto-robot/websocket_control_server.h
小鹏 67bf599149 fix(otto): WebSocket direct clients not receiving MCP responses (#1992)
* Enhance Otto Robot camera support by adding configuration for OV3660. Updated config.h to define camera types and GPIO settings, modified config.json to include new camera options, and refactored otto_robot.cc for improved camera detection and initialization logic.

* fix: 移除 OttoEmojiDisplay 构造函数中的 SetTheme 调用以修复 LoadProhibited 崩溃

Made-with: Cursor

* refactor: improve audio service error handling and codec timeout management

- Updated AudioService to prevent input task termination on read timeout, introducing a delay instead.
- Enhanced NoAudioCodec to implement a read timeout for I2S channel reads.
- Adjusted WebSocketControlServer to set a control port for improved socket management.
- Added manufacturer information to the config.json for waveshare ESP32-Touch-LCD-3.5.

* fix(otto): WebSocket direct clients not receiving MCP responses

When a browser connects directly to the WebSocket control server (port
8080) and sends a JSON-RPC request, the MCP response was routed through
Application::SendMcpMessage -> protocol_->SendMcpMessage, which sends it
to the cloud protocol channel. As a result, the direct WebSocket client
never received the response, while the WeChat mini-program could because
it communicates via the cloud.

Fix:
- Add BroadcastMessage() to WebSocketControlServer, using
  httpd_queue_work + httpd_ws_send_frame_async to asynchronously
  send responses back to all connected clients on port 8080
- Add RegisterMcpBroadcastCallback() to Application, allowing an
  additional MCP send callback to be registered; SendMcpMessage()
  now invokes it alongside the cloud protocol
- Register the broadcast callback in OttoRobot after the WebSocket
  server starts successfully

Also add WebSocket direct-connect API documentation to README.md
with complete JSON-RPC 2.0 command examples.
2026-05-14 14:35:49 +08:00

36 lines
784 B
C++

#ifndef WEBSOCKET_CONTROL_SERVER_H
#define WEBSOCKET_CONTROL_SERVER_H
#include <esp_http_server.h>
#include <cJSON.h>
#include <string>
#include <map>
class WebSocketControlServer {
public:
WebSocketControlServer();
~WebSocketControlServer();
bool Start(int port = 8080);
void Stop();
size_t GetClientCount() const;
void BroadcastMessage(const std::string& message);
private:
httpd_handle_t server_handle_;
std::map<int, httpd_req_t*> clients_;
static esp_err_t ws_handler(httpd_req_t *req);
void HandleMessage(httpd_req_t *req, const char* data, size_t len);
void AddClient(httpd_req_t *req);
void RemoveClient(httpd_req_t *req);
static WebSocketControlServer* instance_;
};
#endif // WEBSOCKET_CONTROL_SERVER_H