8266使用websocket库

安装 WebSocket 库

  1. 使用 Arduino IDE 安装

    • 打开 Arduino IDE。

    • 转到 Sketch > Include Library > Manage Libraries...

    • 在搜索框中输入"WebSockets"并查找 WebSockets by Markus Sattler 的库。

    • 点击安装。

  2. 最下面那个,安装

  3. 编译运行测试,通过,没问题

下来我贴一些测试代码

复制代码
#include <WebSocketsServer.h>
#include <ESP8266WiFi.h>

// 替换为你的WiFi网络凭据
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";

// WebSocket服务器端口
WebSocketsServer webSocket = WebSocketsServer(81);

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  // 启动WebSocket服务器
  webSocket.begin();
  webSocket.on("message", handleWebSocketMessage);
  Serial.println("WebSockets started");
}

void loop() {
  webSocket.loop();
}

void handleWebSocketMessage(uint8_t num, WStype_t type, uint8_t *payload, size_t length) {
  if (type == WStype_TEXT) {
    Serial.printf("Received message: %s\n", payload);
    webSocket.sendTXT(num, "Message received");
  }
}
相关推荐
LUCIAZZZ5 分钟前
HTTPS优化简单总结
网络·网络协议·计算机网络·http·https·操作系统
wanhengidc17 分钟前
云手机运行流畅,秒开不卡顿
运维·网络·科技·游戏·智能手机
名誉寒冰1 小时前
TCP 拥塞控制与四次挥手解析
网络·网络协议·tcp/ip
笨小孩@GF 知行合一1 小时前
OSPF实验:外部路由引入
运维·网络·hcip·数通·ospf
Fireplusplus1 小时前
TCP MSS
网络·网络协议·tcp/ip
asdfg12589631 小时前
为什么要在出口路由器router配置NAT与默认路由
运维·网络·计算机网络
希赛网1 小时前
华为认证HCIA备考知识点 :IP路由基础(含配置案例)
网络·网络协议·计算机网络·路由交换
toooooop81 小时前
本地开发环境webScoket调试,保存html即用
前端·css·websocket
青草地溪水旁2 小时前
http response的工作流程详细解析
网络协议·http·应答
当归10242 小时前
SQL Server死锁排查实战指南
java·服务器·网络