This is my ESP32 code,
#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);
}
This is the server code running on 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()}`;
}
When I run the server locally on my PC, ESP32 connects to the server. But when I deploy my server to Heroku and try to connect the ESP32 handshake fail occurs. This is the output on the serial monitor I get
Connected
Waiting...
Waiting...
Waiting...
Handshake failed.
Client disconnected.
Connected
Waiting...
Waiting...
Waiting...
Handshake failed.
Client disconnected.
Connected
Waiting...
Waiting...
Waiting...
Waiting...
Waiting...
Handshake failed.
There is no problem with the deployment to server because when I use a nodejs websocket client(example from the repo) it successfully connects to the deployed Heroku server. I have tried changing port number to 80 and using 'http://hidden-thicket-03510.herokuapp.com' on the Arduino script, but it still didn't work.
Why is my ESP32 able to connect to the locally running server, but not able to connect when deployed to the server
P.S - the above server is still working if you want to test it out.
Any help will be appreciated!!
question from:
https://stackoverflow.com/questions/65931598/cant-connect-esp32-to-websocket-server-deployed-to-heroku 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…