@@ -19,6 +19,12 @@ namespace remote_webview {
1919static const char *const TAG = " Remote_WebView" ;
2020RemoteWebView *RemoteWebView::self_ = nullptr ;
2121
22+ static inline void websocket_force_reconnect (esp_websocket_client_handle_t client) {
23+ if (!client) return ;
24+ esp_websocket_client_stop (client);
25+ esp_websocket_client_start (client);
26+ }
27+
2228void RemoteWebView::setup () {
2329 self_ = this ;
2430
@@ -156,7 +162,12 @@ void RemoteWebView::ws_task_tramp_(void *arg) {
156162 ESP_ERROR_CHECK (esp_websocket_client_start (client));
157163
158164 for (;;) {
159- vTaskDelay (pdMS_TO_TICKS (1000 ));
165+ vTaskDelay (pdMS_TO_TICKS (5000 ));
166+
167+ if (!esp_websocket_client_is_connected (client)) {
168+ websocket_force_reconnect (client);
169+ continue ;
170+ }
160171
161172 if (self && self->ws_client_ && esp_websocket_client_is_connected (self->ws_client_ )) {
162173 const uint64_t now = esp_timer_get_time ();
@@ -195,8 +206,19 @@ void RemoteWebView::ws_event_handler_(void *handler_arg, esp_event_base_t, int32
195206 ESP_LOGI (TAG, " [ws] disconnected" );
196207 if (self_) self_->last_keepalive_us_ = 0 ;
197208 reasm_reset_ (*r);
209+ websocket_force_reconnect (e->client );
198210 break ;
199211
212+ #ifdef WEBSOCKET_EVENT_CLOSED
213+ case WEBSOCKET_EVENT_CLOSED:
214+ if (self_) self_->ws_client_ = nullptr ;
215+ ESP_LOGI (TAG, " [ws] closed" );
216+ if (self_) self_->last_keepalive_us_ = 0 ;
217+ reasm_reset_ (*r);
218+ websocket_force_reconnect (e->client );
219+ break ;
220+ #endif
221+
200222 case WEBSOCKET_EVENT_DATA: {
201223 if (!self_) break ;
202224
@@ -444,6 +466,9 @@ int RemoteWebView::jpeg_draw_cb_(JPEGDRAW *p) {
444466}
445467
446468bool RemoteWebView::ws_send_touch_event_ (proto::TouchType type, int x, int y, uint8_t pid) {
469+ if (touch_disabled_)
470+ return false ;
471+
447472 if (!ws_client_ || !ws_send_mtx_ || !esp_websocket_client_is_connected (ws_client_))
448473 return false ;
449474
@@ -462,11 +487,6 @@ bool RemoteWebView::ws_send_touch_event_(proto::TouchType type, int x, int y, ui
462487 return r == (int )n;
463488}
464489
465- void RemoteWebViewTouchListener::touch (touchscreen::TouchPoint tp) {
466- if (!parent_) return ;
467- parent_->ws_send_touch_event_ (proto::TouchType::Down, tp.x , tp.y , tp.id );
468- }
469-
470490bool RemoteWebView::ws_send_open_url_ (const char *url, uint16_t flags) {
471491 if (!ws_client_ || !ws_send_mtx_ || !url || !esp_websocket_client_is_connected (ws_client_))
472492 return false ;
@@ -510,10 +530,9 @@ bool RemoteWebView::ws_send_keepalive_() {
510530 return r == (int )n;
511531}
512532
513-
514-
515533void RemoteWebViewTouchListener::update (const touchscreen::TouchPoints_t &pts) {
516534 if (!parent_) return ;
535+
517536 const uint64_t now = esp_timer_get_time ();
518537 for (auto &p : pts) {
519538 switch (p.state ) {
@@ -542,6 +561,17 @@ void RemoteWebViewTouchListener::release() {
542561 parent_->ws_send_touch_event_ (proto::TouchType::Up, 0 , 0 , 0 );
543562}
544563
564+ void RemoteWebViewTouchListener::touch (touchscreen::TouchPoint tp) {
565+ if (!parent_) return ;
566+
567+ parent_->ws_send_touch_event_ (proto::TouchType::Down, tp.x , tp.y , tp.id );
568+ }
569+
570+ void RemoteWebView::disable_touch (bool disable) {
571+ touch_disabled_ = disable;
572+ ESP_LOGD (TAG, " touch %s" , disable ? " disabled" : " enabled" );
573+ }
574+
545575void RemoteWebView::set_server (const std::string &s) {
546576 auto pos = s.rfind (' :' );
547577 if (pos == std::string::npos || pos == s.size () - 1 ) {
0 commit comments