towerops/lib/towerops_web/channels/agent_socket.ex

34 lines
835 B
Elixir

defmodule ToweropsWeb.AgentSocket do
@moduledoc """
WebSocket endpoint for remote agent communication.
Agents connect to: ws://server/socket/agent?token=<agent_token>
Uses binary Protocol Buffers encoding for all messages.
"""
use Phoenix.Socket
channel "agent:*", ToweropsWeb.AgentChannel
@impl true
def connect(%{"token" => token}, socket, _connect_info) do
case Towerops.Agents.verify_agent_token(token) do
{:ok, agent_token} ->
socket =
socket
|> assign(:agent_token_id, agent_token.id)
|> assign(:organization_id, agent_token.organization_id)
{:ok, socket}
{:error, _} ->
:error
end
end
def connect(_, _socket, _connect_info), do: :error
@impl true
def id(socket), do: "agent_socket:#{socket.assigns.agent_token_id}"
end