Skip to content

Commit

Permalink
Merge pull request #510 from Th3-M4jor/fix-reconnect-requests
Browse files Browse the repository at this point in the history
Fix reconnect requests
  • Loading branch information
jchristgit authored Jun 14, 2023
2 parents fb9faef + 046754c commit 429a7d2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/nostrum/shard/event.ex
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ defmodule Nostrum.Shard.Event do

def handle(:reconnect, _payload, state) do
Logger.info("RECONNECT")
{state, []}
{{state, :reconnect}, []}
end

def handle(event, _payload, state) do
Expand Down
25 changes: 18 additions & 7 deletions lib/nostrum/shard/session.ex
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,20 @@ defmodule Nostrum.Shard.Session do
{:next_state, :connecting_http, data}
end

#def disconnected(_kind, _request, _data) do
# def disconnected(_kind, _request, _data) do
# {:keep_state_and_data, :postpone}
#end
# end

# If we've been here before, we want to use the resume gateway URL to connect
# instead of the regular gateway URL.
def connecting_http(:enter, from, %{resume_gateway: resume_gateway} = data)
when resume_gateway != nil do
#
# We also have to strip the "wss://" prefix from the URL, as `:gun` doesn't
# currently understand it.
def connecting_http(:enter, from, %{resume_gateway: "wss://" <> resume_gateway} = data) do
Logger.debug("Resuming on #{inspect(resume_gateway)}")
connecting_http(:enter, from, %{data | gateway: resume_gateway})

# if we don't set resume_gateway to nil, we'll have an infinite loop
connecting_http(:enter, from, %{data | gateway: resume_gateway, resume_gateway: nil})
end

def connecting_http(:enter, _from, %{gateway: gateway} = data) do
Expand Down Expand Up @@ -183,9 +187,9 @@ defmodule Nostrum.Shard.Session do
{:stop, :connect_http_timeout}
end

#def connecting_http(_kind, _request, _data) do
# def connecting_http(_kind, _request, _data) do
# {:keep_state_and_data, :postpone}
#end
# end

def connecting_ws(:enter, _from, %{conn: conn} = data) do
Logger.debug("Upgrading connection to websocket")
Expand Down Expand Up @@ -250,6 +254,13 @@ defmodule Nostrum.Shard.Session do
:ok = :gun.update_flow(data_with_seq.conn, stream, @standard_flow)

case from_handle do
{updated_data, :reconnect} ->
:gun.close(data_with_seq.conn)
:gun.flush(data_with_seq.conn)
connect = {:next_event, :internal, :connect}
new_data = %{updated_data | conn: nil, stream: nil}
{:next_state, :disconnected, new_data, connect}

{updated_data, reply} ->
:ok = :gun.ws_send(data_with_seq.conn, stream, {:binary, reply})
{:keep_state, updated_data, heartbeat_actions}
Expand Down

0 comments on commit 429a7d2

Please # to comment.