logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://anongit.hacktivis.me/git/pleroma.git/

websocket_client.ex (1369B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Integration.WebsocketClient do
  5. # https://github.com/phoenixframework/phoenix/blob/master/test/support/websocket_client.exs
  6. use WebSockex
  7. @doc """
  8. Starts the WebSocket server for given ws URL. Received Socket.Message's
  9. are forwarded to the sender pid
  10. """
  11. def start_link(sender, url, headers \\ []) do
  12. WebSockex.start_link(
  13. url,
  14. __MODULE__,
  15. %{sender: sender},
  16. extra_headers: headers
  17. )
  18. end
  19. @doc """
  20. Closes the socket
  21. """
  22. def close(socket) do
  23. send(socket, :close)
  24. end
  25. @doc """
  26. Sends a low-level text message to the client.
  27. """
  28. def send_text(server_pid, msg) do
  29. send(server_pid, {:text, msg})
  30. end
  31. @doc false
  32. @impl true
  33. def handle_frame(frame, state) do
  34. send(state.sender, frame)
  35. {:ok, state}
  36. end
  37. @impl true
  38. def handle_disconnect(conn_status, state) do
  39. send(state.sender, {:close, conn_status})
  40. {:ok, state}
  41. end
  42. @doc false
  43. @impl true
  44. def handle_info({:text, msg}, state) do
  45. {:reply, {:text, msg}, state}
  46. end
  47. @impl true
  48. def handle_info(:close, _state) do
  49. {:close, <<>>, "done"}
  50. end
  51. @doc false
  52. @impl true
  53. def terminate(_reason, _state) do
  54. :ok
  55. end
  56. end