From 2ca9b975dc9a8bb177526f7f8e54a0f60cac5ac6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Ro=C5=BCnawski?= <48837433+roznawsk@users.noreply.github.com> Date: Tue, 16 Jan 2024 13:24:19 +0100 Subject: [PATCH] [RTC-436] Propagate websocket close reason (#140) * Send reason when closing WS * Revert deps upgrade * Normal text messagess * Fix tests --- lib/jellyfish_web/peer_socket.ex | 14 ++++++++++---- .../jellyfish_web/integration/peer_socket_test.exs | 11 +++++++---- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/lib/jellyfish_web/peer_socket.ex b/lib/jellyfish_web/peer_socket.ex index 4bb45eac..b7b1e62e 100644 --- a/lib/jellyfish_web/peer_socket.ex +++ b/lib/jellyfish_web/peer_socket.ex @@ -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)}, \ @@ -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 diff --git a/test/jellyfish_web/integration/peer_socket_test.exs b/test/jellyfish_web/integration/peer_socket_test.exs index 25fdeae6..7cedc381 100644 --- a/test/jellyfish_web/integration/peer_socket_test.exs +++ b/test/jellyfish_web/integration/peer_socket_test.exs @@ -149,15 +149,18 @@ 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 @@ -165,7 +168,7 @@ defmodule JellyfishWeb.Integration.PeerSocketTest do 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