Cimera42 / esp-discord-client

Discord WebSocket client running on ESP8266 or ESP32
MIT License
30 stars 2 forks source link

Stuck on connecting #12

Closed FredFronky closed 3 years ago

FredFronky commented 3 years ago

Hi there, This looks like a really cool project, but I can't get it working - maybe Discord's changed something on their end?

The code continuously prints "connecting" to the serial output. Looks like the client=>connect in WebSocketClient.cpp is never returning true.

bool WebSocketClient::connect(String host, String path, int port) {
    DEBUG_WS(host.c_str());

    if (!client->connect(host.c_str(), port))
        return false; //always returns here

Is this working for anyone else, or is it something specific to my environment? I can make regular http get requests from the same code, so the board has an internet connection.

Cimera42 commented 3 years ago

Hi @FredFronky, Thanks for the interest in this :) Just tested it on my board and it's able to connect fine.

A few things that it might be:

I've included a copy of the logs from my board (with personal data removed) when it connects successfully for you to compare to:

Connecting to <wifi-name>
.
WiFi connected
IP address: 
192.168.1.8
connecting
[WS] sending handshake
GET https://gateway.discord.gg/ HTTP/1.1
Host: gateway.discord.gg
Connection: Upgrade
Upgrade: websocket
Sec-WebSocket-Version: 13
Sec-WebSocket-Key: D9o9Rr8d7N9uxsQAIim87qy=

[WS][RX] HTTP/1.1 101 Switching Protocols

[WS][RX] Date: Sun, 21 Feb 2021 10:12:02 GMT

[WS][RX] Connection: upgrade

[WS][RX] Set-Cookie: __cfduid=da4561bf40e05b4d31294a82139bd7f9f1613902322; expires=Tue, 23-Mar-21 10:12:02 GMT; path=/; domain=.discord.gg; HttpOnly; SameSite=Lax; Secure

[WS][RX] sec-websocket-accept: /8RZVCG/lSo7/7+bfWt0EKqeNuk=

[WS][RX] upgrade: websocket

[WS][RX] CF-Cache-Status: DYNAMIC

[WS][RX] cf-request-id: 0865ac323d0000fd3ae2a92000000001

[WS][RX] Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"

[WS][RX] Report-To: {"endpoints":[{"url":"https:\/\/a.nel.cloudflare.com\/report?s=1iNfag9Ay8rsBrHzpB5K%2BT9Dmmfyw0gqlGjvnDLZOQ2UGe5fCmE7kv7ch1%2Febz9%2FM6fjw80WgLE4EQDzz8AIGUVPMfE1Bwtl6UEUC2MK8wla2I8%3D"}],"group":"cf-nel","max_age":604800}

[WS][RX] NEL: {"report_to":"cf-nel","max_age":604800}

[WS][RX] Strict-Transport-Security: max-age=31536000; includeSubDomains; preload

[WS][RX] X-Content-Type-Options: nosniff

[WS][RX] Server: cloudflare

[WS][RX] CF-RAY: 624fafc9f9befd3a-SYD

[WS][RX] 

[WS] sucessfully connected
{"t":null,"s":null,"op":10,"d":{"heartbeat_interval":41250,"_trace":["[\"gateway-prd-main-ktwx\",{\"micros\":0.0}]"]}}
Send:: {"op":2,"d":{"token":"<bot-token>","properties":{"$os":"linux","$browser":"ESP8266","$device":"ESP8266"},"compress":false,"large_threshold":250}}
[WS] sending: {"op":2,"d":{"token":"<bot-token>","properties":{"$os":"linux","$browser":"ESP8266","$device":"ESP8266"},"compress":false,"large_threshold":250}}
{"t":"READY","s":1,"op":0,"d":{"v":6,"user_settings":{},"user":<user details>,"session_id":<session id>,"relationships":[],"private_channels":[],"presences":[],"guilds":[{"unavailable":true,"id":<guild id>}],"guild_join_requests":[],"geo_ordered_rtc_regions":["sydney","singapore","hongkong","japan","india"],"application":{"id":<application id>,"flags":0},"_trace":["[\"gateway-prd-main-p7kr\",{\"micros\":120470,\"calls\":[\"discord-sessions-prd-2-75\",{\"micros\":113314,\"calls\":[\"start_session\",{\"micros\":85676,\"calls\":[\"api-prd-main-swtm\",{\"micros\":73861,\"calls\":[\"get_user\",{\"micros\":6084},\"add_authorized_ip\",{\"micros\":1315},\"get_guilds\",{\"micros\":8081},\"coros_wait\",{\"micros\":0}]}]},\"guilds_connect\",{\"micros\":2,\"calls\":[]},\"presence_connect\",{\"micros\":0,\"calls\":[]}]}]}]"]}}
{"t":"GUILD_CREATE","s":2,"op":0,"d":<guild data> }
Cimera42 commented 3 years ago

Hiya,

I've added gateway intents in #14, as well as specifying gateway version 8. Try pulling master now and see if it solves your problem. I've got an example set of intents in there, but you can change it to whatever your application needs.

Let me know how it goes.

FredFronky commented 3 years ago

Thanks for your super helpful reply. I had a minute tonight to have a quick play with it, and it looks like it could be a combination of two problems. I tried initially on an ESP8266 and then ESP32, with no luck. With the latest update in #14, the ESP8266 still sits on connecting, but my ESP32 is able to connect after a few retries. Not really sure what's going on there, but it looks like it's working for now. I'll have a deeper look and see if I can identify the problem sometime this weekend. I don't remember where I got this particular ESP8266 - it hasn't caused any issues before - but I might track down another board and see if I can recreate it. Out of interest, what are you using? Thanks again for your time

Cimera42 commented 3 years ago

Awesome, good to hear the changes have had some effect.

I'm using a ESP8266 board I bought from Ebay like 3 years ago.

https://www.ebay.com.au/itm/182557571426

Lohkdesgds commented 3 years ago

If you call setInsecure() on client, it'll work. The thing about connecting being stuck is because the ESP can't check the certificates (I don't know why), so with that can "ignore" this check and just accept it as is.

By the way is there a Discord group or something? It would be nice ;P

cmd420 commented 3 years ago

I still have the same thing, stuck on connecting, idk what's wrong.

Cimera42 commented 3 years ago

I still have the same thing, stuck on connecting, idk what's wrong.

Thanks for the bump on this @ahmedo-o

I've updated my ESP8266 sdk version to v2.7.4 and was getting stuck on connecting too. (Thanks chineseouchie on Reddit for the tip about the version)

As @Lohkdesgds said, it's to do with the SSL certificates not validating. Changing from v2.4.2 to v2.7.4, it seems to now be enabled by default. I've added this certificate validation in #15, and it should connect properly now.

P.S. Config has now moved to config.h and privateConfig.h

Let me know how you go 🙂

cmd420 commented 3 years ago

tested it and it works flawlessly!

Cimera42 commented 3 years ago

Gonna say this was closed by #15. If anyone has more problems, feel free to create a new issue and let me know the details.