2018-12-23 20:11:29 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-13 06:49:20 +00:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2018-12-23 20:11:29 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2018-12-17 16:09:06 +00:00
|
|
|
defmodule Pleroma.Integration.WebsocketClient do
|
|
|
|
# https://github.com/phoenixframework/phoenix/blob/master/test/support/websocket_client.exs
|
|
|
|
|
2022-08-19 18:19:38 +01:00
|
|
|
use WebSockex
|
|
|
|
|
2018-12-17 16:09:06 +00:00
|
|
|
@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
|
2022-08-19 18:19:38 +01:00
|
|
|
WebSockex.start_link(
|
|
|
|
url,
|
2018-12-17 16:09:06 +00:00
|
|
|
__MODULE__,
|
2022-08-19 18:19:38 +01:00
|
|
|
%{sender: sender},
|
2018-12-17 16:09:06 +00:00
|
|
|
extra_headers: headers
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Closes the socket
|
|
|
|
"""
|
|
|
|
def close(socket) do
|
|
|
|
send(socket, :close)
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Sends a low-level text message to the client.
|
|
|
|
"""
|
|
|
|
def send_text(server_pid, msg) do
|
|
|
|
send(server_pid, {:text, msg})
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc false
|
2022-08-19 18:19:38 +01:00
|
|
|
@impl true
|
|
|
|
def handle_frame(frame, state) do
|
|
|
|
send(state.sender, frame)
|
|
|
|
{:ok, state}
|
2018-12-17 16:09:06 +00:00
|
|
|
end
|
|
|
|
|
2022-08-19 18:19:38 +01:00
|
|
|
@impl true
|
|
|
|
def handle_disconnect(conn_status, state) do
|
|
|
|
send(state.sender, {:close, conn_status})
|
2018-12-17 16:09:06 +00:00
|
|
|
{:ok, state}
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc false
|
2022-08-19 18:19:38 +01:00
|
|
|
@impl true
|
|
|
|
def handle_info({:text, msg}, state) do
|
2018-12-17 16:09:06 +00:00
|
|
|
{:reply, {:text, msg}, state}
|
|
|
|
end
|
|
|
|
|
2022-08-19 18:19:38 +01:00
|
|
|
@impl true
|
|
|
|
def handle_info(:close, _state) do
|
2018-12-17 16:09:06 +00:00
|
|
|
{:close, <<>>, "done"}
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc false
|
2022-08-19 18:19:38 +01:00
|
|
|
@impl true
|
|
|
|
def terminate(_reason, _state) do
|
2018-12-17 16:09:06 +00:00
|
|
|
:ok
|
|
|
|
end
|
|
|
|
end
|