Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 使用socket.io将ESP32连接到nodejs服务器中的特定房间_Node.js_Websocket_Socket.io_Arduino_Esp32 - Fatal编程技术网

Node.js 使用socket.io将ESP32连接到nodejs服务器中的特定房间

Node.js 使用socket.io将ESP32连接到nodejs服务器中的特定房间,node.js,websocket,socket.io,arduino,esp32,Node.js,Websocket,Socket.io,Arduino,Esp32,我正在使用arduino为我的ESP32编程。这是我正在使用的代码,似乎我能够连接到websocket服务器 #include <WiFi.h> #include <WebSocketClient.h> const char* ssid = "#######"; const char* password = "####"; char path[] = "/"; char host[] = &quo

我正在使用arduino为我的ESP32编程。这是我正在使用的代码,似乎我能够连接到websocket服务器

#include <WiFi.h>
#include <WebSocketClient.h>
 
const char* ssid     = "#######";
const char* password = "####";
 
char path[] = "/";
char host[] = "192.168.#.##";
 
WebSocketClient webSocketClient;
WiFiClient client;

void connnect(){
   if (client.connect(host, 5000)) {
    Serial.println("Connected");
  } else {
    Serial.println("Connection failed.");
  }
 
  webSocketClient.path = path;
  webSocketClient.host = host;
  if (webSocketClient.handshake(client)) {
    Serial.println("Handshake successful");
  } else {
    Serial.println("Handshake failed.");
  }
}
void setup() {
  Serial.begin(115200);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
 
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
 
  delay(5000);
 
 connnect();
 
}
 
void loop() {
  String data;
 
  if (client.connected()) {
 
    webSocketClient.sendData("Info to be echoed back");
 
    webSocketClient.getData(data);
    if (data.length() > 0) {
      Serial.print("Received data: ");
      Serial.println(data);
    }
 
  } else {
    Serial.println("Client disconnected.");
    connnect();
  }
 
  delay(3000);
 
}

#包括
#包括
const char*ssid=“########”;
const char*password=“#####”;
字符路径[]=“/”;
字符主机[]=“192.168.#.##”;
WebSocketClient WebSocketClient;
无线客户端;
void connnect(){
if(客户端连接(主机,5000)){
Serial.println(“已连接”);
}否则{
Serial.println(“连接失败”);
}
webSocketClient.path=路径;
webSocketClient.host=主机;
if(webSocketClient.handshake(客户端)){
Serial.println(“握手成功”);
}否则{
Serial.println(“握手失败”);
}
}
无效设置(){
序列号开始(115200);
WiFi.begin(ssid,密码);
while(WiFi.status()!=WL_已连接){
延迟(500);
连续打印(“.”);
}
Serial.println(“”);
Serial.println(“WiFi连接”);
Serial.println(“IP地址:”);
Serial.println(WiFi.localIP());
延迟(5000);
连接();
}
void循环(){
字符串数据;
if(client.connected()){
sendData(“要回显的信息”);
webSocketClient.getData(数据);
if(data.length()>0){
串行打印(“接收数据:”);
Serial.println(数据);
}
}否则{
Serial.println(“客户端断开连接”);
连接();
}
延迟(3000);
}
在服务器端,我有一个带有socket.io的nodejs express服务器。我想做的是用ESP32连接到一个特定的房间。我怎样才能连接到房间。在这段代码中我必须做的更改是什么


感谢您的帮助

socket.io与WebSocket不“兼容”。io使用WebSocket与服务器通信,但不是普通的WebSocket。它是ws之上的一个协议。您可以在服务器端切换到普通websocket。您是否可以推荐构建ESP32 websocket客户端和Nodejs websocket服务器的任何启动提示对我来说,您有一个适用于esp的ws实现(使用
WebSocketClient.h
)。我可以在/with
node.js
@Marc中为您发布一个简单的ws-server,如果您能够做到这一点,它将非常有用。Socket.io与WebSocket不“兼容”。io使用WebSocket与服务器通信,但不是普通的WebSocket。它是ws之上的一个协议。您可以在服务器端切换到普通websocket。您是否可以推荐构建ESP32 websocket客户端和Nodejs websocket服务器的任何启动提示对我来说,您有一个适用于esp的ws实现(使用
WebSocketClient.h
)。我可以在/with
node.js
@Marc中为您发布一个简单的ws-server,如果您能够做到这一点,它将非常有用