Current section

Files

Jump to
soulless lib soulless websocket implementation chat.ex
Raw

lib/soulless/websocket/implementation/chat.ex

defmodule Soulless.Websocket.Implementation.Chat do
alias Soulless.Packet.TourneyChat
@behaviour Soulless.Websocket.Implementation
@required_credentials [:token]
@impl Soulless.Websocket.Implementation
def init(opts) do
endpoint_uri = Keyword.fetch!(opts, :endpoint_uri)
opts
|> Keyword.take(@required_credentials)
|> Keyword.put(:endpoint_uri, endpoint_uri)
|> Map.new()
end
@impl Soulless.Websocket.Implementation
def fetch_state(state, path) do
get_in(state, path)
end
@impl Soulless.Websocket.Implementation
def endpoint_uri(state) do
state.endpoint_uri
end
@impl Soulless.Websocket.Implementation
def resolve_endpoint(state) do
state.endpoint_uri
query = %{
"message_id" => "0",
"stream" => "binary",
"support_id" => "true",
"system_id" => "0",
"token" => state.token
}
endpoint_uri = Map.replace!(state.endpoint_uri, :query, query)
{:ok, %{state | endpoint_uri: endpoint_uri}}
end
@impl Soulless.Websocket.Implementation
def handle_auth_ready(_state) do
{:error, :should_never_happen}
end
@impl Soulless.Websocket.Implementation
def handle_auth_message(_message, state) do
{:done, state}
end
@impl Soulless.Websocket.Implementation
def handle_ready(_client, state) do
{:ok, state}
end
@impl Soulless.Websocket.Implementation
def handle_disconnect(_client, _reason, state) do
{:reconnect, state}
end
@impl Soulless.Websocket.Implementation
def packet_request_id(decoded) do
decoded.request_id
end
@impl Soulless.Websocket.Implementation
def packet_body(decoded) do
decoded.body
end
@impl Soulless.Websocket.Implementation
def handle_encode(_request_id, _payload, _state) do
raise "sending messages via tourney chat socket is not supported"
end
@impl Soulless.Websocket.Implementation
def handle_decode(_frame_type, data, state) do
with {:ok, packet} <- TourneyChat.parse(data) do
{:ok, {packet, state}}
end
end
end