Skip to content

Commit

Permalink
[RTC-436] Propagate websocket close reason (#140)
Browse files Browse the repository at this point in the history
* Send reason when closing WS

* Revert deps upgrade

* Normal text messagess

* Fix tests
  • Loading branch information
roznawsk authored Jan 16, 2024
1 parent fe17b8c commit 2ca9b97
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
14 changes: 10 additions & 4 deletions lib/jellyfish_web/peer_socket.ex
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ defmodule JellyfishWeb.PeerSocket do
{:ok, state}
end

@impl true
def handle_in({msg, [opcode: :text]}, state) do
Logger.warning("""
Received unexpected text message #{msg} from #{inspect(state.peer_id)}, \
Expand All @@ -113,18 +114,23 @@ defmodule JellyfishWeb.PeerSocket do
end

@impl true
def handle_info({:stop_connection, reason}, state) do
{:stop, reason, state}
def handle_info({:stop_connection, :peer_removed}, state) do
{:stop, :closed, {1000, "Peer removed"}, state}
end

@impl true
def handle_info({:stop_connection, _reason}, state) do
{:stop, :closed, {1011, "Internal server error"}, state}
end

@impl true
def handle_info(:room_crashed, state) do
{:stop, :room_crashed, state}
{:stop, :closed, {1011, "Internal server error"}, state}
end

@impl true
def handle_info(:room_stopped, state) do
{:stop, :room_stopped, state}
{:stop, :closed, {1000, "Room stopped"}, state}
end

@impl true
Expand Down
11 changes: 7 additions & 4 deletions test/jellyfish_web/integration/peer_socket_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -149,23 +149,26 @@ defmodule JellyfishWeb.Integration.PeerSocketTest do
create_and_authenticate(token)

_conn = delete(conn, ~p"/room/#{room_id}/peer/#{peer_id}")
assert_receive {:disconnected, {:remote, 1000, ""}}, 1000
assert_receive {:disconnected, {:remote, 1000, "Peer removed"}}, 1000
end

test "room crash", %{room_pid: room_pid, token: token} do
create_and_authenticate(token)
ws = create_and_authenticate(token)
Process.unlink(ws)
ref = Process.monitor(ws)

Process.exit(room_pid, :error)

assert_receive {:disconnected, {:remote, 1000, ""}}, 1000
assert_receive {:disconnected, {:remote, 1011, "Internal server error"}}, 1000
assert_receive {:DOWN, ^ref, :process, ^ws, {:remote, 1011, "Internal server error"}}
end

test "room close", %{room_id: room_id, token: token, conn: conn} do
create_and_authenticate(token)
conn = delete(conn, ~p"/room/#{room_id}/")
response(conn, :no_content)

assert_receive {:disconnected, {:remote, 1000, ""}}, 1000
assert_receive {:disconnected, {:remote, 1000, "Room stopped"}}, 1000
end

def create_and_authenticate(token) do
Expand Down

0 comments on commit 2ca9b97

Please sign in to comment.