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