client.connect()附加参数-查询



我使用ESP32作为socket.io服务器应用程序的客户端。我需要ESP32在"连接"方法中发送MAC地址作为查询。在我的浏览器中,我可以做:

io('localhost:2000', {query: { mac: 222222 }})

我需要ESP32来做同样的事情,但我仅限于WiFiClient库,我可以使用它与服务器进行基本连接:

client.connect(host, port)

但我想发送额外的信息,例如:

client.connect(host, port, query)

为了查询内容,您必须准备HTTP请求,并使用println函数对其进行描述。你可以看到下面的例子,摘自官方网站:

#include <SPI.h>
#include <WiFi.h>
char ssid[] = "myNetwork";          //  your network SSID (name)
char pass[] = "myPassword";   // your network password
int status = WL_IDLE_STATUS;
IPAddress server(74,125,115,105);  // Google
// Initialize the client library
WiFiClient client;
void setup() {
Serial.begin(9600);
Serial.println("Attempting to connect to WPA network...");
Serial.print("SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
if ( status != WL_CONNECTED) {
Serial.println("Couldn't get a wifi connection");
// don't do anything else:
while(true);
}
else {
Serial.println("Connected to wifi");
Serial.println("nStarting connection...");
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("connected");
// Make a HTTP request:
//Here you have to mount your "query" or specify your JSON body (HTTP protocol):
client.println("GET /search?q=arduino HTTP/1.0"); 
client.println();
}
}
}

您可以在这里、这里(这是一个POST示例,但它很有用(和这里找到更多的示例。

最新更新