From 16c0b34f73ad2c54defd9e2791bd8c15e27e35e0 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Wed, 23 Dec 2020 20:21:40 +0300 Subject: Discord: finally fixes #2499 (Discord: Error establishing WebSocket connection to gateway.discord.gg) --- protocols/Discord/src/gateway.cpp | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) (limited to 'protocols/Discord/src') diff --git a/protocols/Discord/src/gateway.cpp b/protocols/Discord/src/gateway.cpp index 01fefac5db..c65492569d 100644 --- a/protocols/Discord/src/gateway.cpp +++ b/protocols/Discord/src/gateway.cpp @@ -45,18 +45,35 @@ bool CDiscordProto::GatewayThreadWorker() NETLIBHTTPHEADER hdrs[] = { { "Origin", "https://discord.com" }, - { 0, 0 } + { 0, 0 }, + { 0, 0 }, }; - auto *pReply = WebSocket_Connect(m_hGatewayNetlibUser, m_szGateway + "/?encoding=json&v=8", hdrs); + CMStringA szCookie(getMStringA("WSCookie")); + if (!szCookie.IsEmpty()) { + hdrs[1].szName = "Cookie"; + hdrs[1].szValue = szCookie.GetBuffer(); + } + + NLHR_PTR pReply(WebSocket_Connect(m_hGatewayNetlibUser, m_szGateway + "/?encoding=json&v=8", hdrs)); if (pReply == nullptr) { debugLogA("Gateway connection failed, exiting"); return false; } - + + if (auto *pszNewCookie = Netlib_GetHeader(pReply, "Set-Cookie")) { + char *p = strchr(pszNewCookie, ';'); + if (p) *p = 0; + + setString("WSCookie", pszNewCookie); + } + + if (pReply->resultCode != 101) + return false; + + // succeeded! debugLogA("Gateway connection succeeded"); m_hGatewayConnection = pReply->nlc; - Netlib_FreeHttpRequest(pReply); bool bExit = false; int offset = 0; -- cgit v1.2.3