无法使用ESP8266访问Websocket使用Arduino IDE

问题描述

我一直试图将ESP8266连接到Web套接字,但到目前为止还没有运气。排除的事情是:

  1. ESP8266已连接到Wi-Fi并可以访问Internet(已通过HTTP请求检查)。
  2. 套接字服务器没有问题,我启动了自己的Web套接字服务器,但没有成功。
  3. 尝试使用端口80和443。

我的代码如下,继续打印

未连接!

    #include <Arduino.h>
    #include "WebSocketClient.h"
    #include "ESP8266WiFi.h"
    
    WebSocketClient ws(true);
    
    void setup() {
      Serial.begin(115200);
      WiFi.begin("SSID","PASSWORD");
    
      Serial.print("Connecting");
      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
      }
    }
    
    void loop() {
      if (!ws.isConnected()) {
        ws.connect("echo.websocket.org","/",80);
        Serial.println("Not connected!");
      } else {
        ws.send("hello");
    
        String msg;
        if (ws.getMessage(msg)) {
          Serial.println(msg);
        }
      }
      delay(500);
    }

解决方法

WebSocketClient ws(true);尝试安全连接,并使用WiFiClientSecure代替WiFiClient。这可能需要指定证书或类似的证书。

尝试WebSocketClient ws(false);看看是否可行(不安全的连接)。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...