Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
67 lines (52 sloc) 1.68 KB
defmodule Web.Socket.Router do
@moduledoc """
WebSocket Implementation
Contains server code for the websocket handler
"""
require Logger
alias Socket.Backbone
alias Socket.Core
import Web.Socket.RouterMacro
def backbone_event(state, message) do
Backbone.handle_event(state, message)
end
receives(Socket) do
module(Core, "channels") do
event("heartbeat", :heartbeat)
event("channels/subscribe", :channel_subscribe)
event("channels/unsubscribe", :channel_unsubscribe)
event("channels/send", :channel_send)
end
module(Players, "players") do
event("players/sign-in", :player_sign_in)
event("players/sign-out", :player_sign_out)
event("players/status", :request_status)
end
module(Tells, "tells") do
event("tells/send", :send)
end
module(Games, "games") do
event("games/status", :request_status)
end
end
def receive(state = %{status: "inactive"}, event = %{"event" => "authenticate"}) do
state
|> Core.authenticate(event)
|> Response.wrap(event, "channels")
|> Response.respond_to(state)
end
def receive(state = %{status: "active"}, event = %{"event" => "sync"}) do
state
|> Backbone.sync(event)
|> Response.wrap(event, "sync")
|> Response.respond_to(state)
end
def receive(state = %{status: "inactive"}, frame) do
Logger.warn("Getting an unknown frame unauthenticated - #{inspect(frame)}")
{:ok, %{status: "failure", error: "unauthenticated"}, state}
end
def receive(state, frame) do
Telemetry.execute([:gossip, :sockets, :events, :unknown], 1, %{state: state, frame: frame})
{:ok, %{status: "failure", error: "unknown"}, state}
end
end