【发布时间】:2021-05-02 00:32:38
【问题描述】:
这是我的 ESP32 代码,
#include <WiFi.h>
#include <WebSocketClient.h>
#include <ArduinoJson.h>
const char* ssid = "###";
const char* password = "###";
char path[] = "/";
char host[] = "https://hidden-thicket-03510.herokuapp.com";
WebSocketClient webSocketClient;
WiFiClient client;
int timer=0;
void connnect(){
if (client.connect(host,443)) {
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();
if (client.connected()) {
webSocketClient.sendData("Info to be echoed back");
}
}
void loop() {
String data;
if (client.connected()) {
webSocketClient.getData(data);
Serial.println(data);
int data_len = data.length() + 1;
char char_array[data_len];
data.toCharArray(char_array, data_len);
StaticJsonDocument<1200> doc;
DeserializationError err=deserializeJson(doc,char_array);
const char* a=doc["message"];
if (data_len > 1) {
Serial.print("Received data: ");
Serial.println(a);
}
} else {
Serial.println("Client disconnected.");
connnect();
}
delay(3000);
}
这是在 Heroku 上运行的服务器代码
var WebSocketServer = require('websocket').server;
var http = require('http');
var server = http.createServer(function(request, response) {
console.log((new Date()) + ' Received request for ' + request.url);
response.writeHead(404);
response.end();
});
let port =process.env.PORT || 5000;
server.listen(port, function() {
console.log((new Date()) + ' Server is listening on port 5000');
});
wsServer = new WebSocketServer({
httpServer: server
});
const clients={}
wsServer.on('request', request=> {
var connection = request.accept(null, request.origin);
console.log((new Date()) + ' Connection accepted.');
const clientId=guid();
clients[clientId]={
"connection":connection
};
// connection.sendUTF("JSON.stringify(payload)")
connection.on('open',()=>{console.log("opened")})
connection.on('message', message => {
console.log(message);
var a=JSON.stringify({'message':'sdaed'})
connection.send(a)
});
connection.on('close', (reasonCode, description) =>{
console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.');
});
const payload={
"method":"connect",
"clientId":clientId
}
});
const guid=()=> {
const s4=()=> Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
return `${s4() + s4()}-${s4()}-${s4()}-${s4()}-${s4() + s4() + s4()}`;
}
当我在我的 PC 上本地运行服务器时,ESP32 连接到服务器。但是当我将我的服务器部署到 Heroku 并尝试连接 ESP32 握手失败时发生。这是我得到的串行监视器上的输出
Connected
Waiting...
Waiting...
Waiting...
Handshake failed.
Client disconnected.
Connected
Waiting...
Waiting...
Waiting...
Handshake failed.
Client disconnected.
Connected
Waiting...
Waiting...
Waiting...
Waiting...
Waiting...
Handshake failed.
部署到服务器没有问题,因为当我使用 nodejs websocket 客户端(来自 repo 的示例)时,它成功连接到部署的 Heroku 服务器。我尝试将端口号更改为 80 并在 Arduino 脚本上使用“http://hidden-thicket-03510.herokuapp.com”,但它仍然不起作用。
为什么我的 ESP32 可以连接到本地运行的服务器,但部署到服务器时却无法连接
P.S - 如果你想测试一下,上面的服务器仍然可以工作。
任何帮助将不胜感激!
【问题讨论】:
标签: node.js http heroku websocket esp32