Use Websockex to replace websocket_client

This commit is contained in:
Tusooa Zhu 2022-08-19 13:56:39 -04:00
commit eb42e90c4f
No known key found for this signature in database
GPG key ID: 7B467EDE43A08224
4 changed files with 22 additions and 24 deletions

View file

@ -5,18 +5,17 @@
defmodule Pleroma.Integration.WebsocketClient do
# https://github.com/phoenixframework/phoenix/blob/master/test/support/websocket_client.exs
use WebSockex
@doc """
Starts the WebSocket server for given ws URL. Received Socket.Message's
are forwarded to the sender pid
"""
def start_link(sender, url, headers \\ []) do
:crypto.start()
:ssl.start()
:websocket_client.start_link(
String.to_charlist(url),
WebSockex.start_link(
url,
__MODULE__,
[sender],
%{ sender: sender },
extra_headers: headers
)
end
@ -36,27 +35,26 @@ defmodule Pleroma.Integration.WebsocketClient do
end
@doc false
def init([sender], _conn_state) do
{:ok, %{sender: sender}}
end
@doc false
def websocket_handle(frame, _conn_state, state) do
@impl true
def handle_frame(frame, state) do
send(state.sender, frame)
{:ok, state}
end
@doc false
def websocket_info({:text, msg}, _conn_state, state) do
@impl true
def handle_info({:text, msg}, state) do
{:reply, {:text, msg}, state}
end
def websocket_info(:close, _conn_state, _state) do
@impl true
def handle_info(:close, _state) do
{:close, <<>>, "done"}
end
@doc false
def websocket_terminate(_reason, _conn_state, _state) do
@impl true
def terminate(_reason, _state) do
:ok
end
end