Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.4KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 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. @doc """
  7. Starts the WebSocket server for given ws URL. Received Socket.Message's
  8. are forwarded to the sender pid
  9. """
  10. def start_link(sender, url, headers \\ []) do
  11. :crypto.start()
  12. :ssl.start()
  13. :websocket_client.start_link(
  14. String.to_charlist(url),
  15. __MODULE__,
  16. [sender],
  17. extra_headers: headers
  18. )
  19. end
  20. @doc """
  21. Closes the socket
  22. """
  23. def close(socket) do
  24. send(socket, :close)
  25. end
  26. @doc """
  27. Sends a low-level text message to the client.
  28. """
  29. def send_text(server_pid, msg) do
  30. send(server_pid, {:text, msg})
  31. end
  32. @doc false
  33. def init([sender], _conn_state) do
  34. {:ok, %{sender: sender}}
  35. end
  36. @doc false
  37. def websocket_handle(frame, _conn_state, state) do
  38. send(state.sender, frame)
  39. {:ok, state}
  40. end
  41. @doc false
  42. def websocket_info({:text, msg}, _conn_state, state) do
  43. {:reply, {:text, msg}, state}
  44. end
  45. def websocket_info(:close, _conn_state, _state) do
  46. {:close, <<>>, "done"}
  47. end
  48. @doc false
  49. def websocket_terminate(_reason, _conn_state, _state) do
  50. :ok
  51. end
  52. end