Skip to content

Instantly share code, notes, and snippets.

@nickgartmann
Created October 4, 2014 16:30
Show Gist options
  • Save nickgartmann/02952dc47a1c40e14b44 to your computer and use it in GitHub Desktop.
Save nickgartmann/02952dc47a1c40e14b44 to your computer and use it in GitHub Desktop.
defmodule Pew.Ship do
use Phoenix.Channel
:timer.apply_interval(1000, __MODULE__, :gc, [])
def gc() do
IO.puts "stuff"
end
def join(socket, "join", msg) do
reply socket, "connect", %{ships: Ships.get}
{:ok, socket}
end
def event(socket, "new", data) do
ship = Ship.new(data["x"], data["y"], data["rotation"], data["name"])
Ships.put(ship)
broadcast socket, "new", ship
reply socket, "new:you", ship
socket
end
def event(socket, "update", data) do
ship = Ships.get(data["id"])
broadcast socket, "update", data
# Merge the new data onto the ship, taking into account
# that data has string keys, and ship has atomic keys
ship = Enum.reduce Map.keys(data), ship, fn(el, acc) ->
Map.put(acc, String.to_atom(el), Map.get(data, el))
end
Map.put(ship, :last_update, Timex.Time.now(:msecs))
Ships.put(ship)
socket
end
# Tell everyone to drop the disconnected ship
def event(socket, "disconnect", data) do
Ships.remove(data["id"])
broadcast socket, "remove", data
socket
end
end
@jeregrine
Copy link

def gc() do
   receive
     :gc -> 
          IO.puts "DO stuff
          gc()
    end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment