Created
June 28, 2017 23:21
-
-
Save bmitch/4bcfb83abf830e7afd6ca51c728a7968 to your computer and use it in GitHub Desktop.
ticker.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Ticker do | |
@interval 2000 | |
@name :ticket | |
def start do | |
pid = spawn(__MODULE__, :generator, [[]]) | |
:global.register_name(@name, pid) | |
end | |
def register(client_id) do | |
send :global.whereis_name(@name), { :register, client_id } | |
end | |
def generator(clients) do | |
receive do | |
{ :register, pid } -> | |
IO.puts "registering #{inspect pid}" | |
generator([pid|clients]) | |
after | |
@interval -> | |
IO.puts "tick" | |
Enum.each clients, fn client -> | |
send client, { :tick } | |
end | |
generator(clients) | |
end | |
end | |
end | |
defmodule Client do | |
def start do | |
pid = spawn(__MODULE__, :receiver, []) | |
Ticker.register(pid) | |
end | |
def receiver do | |
receive do | |
{ :tick } -> | |
IO.puts "tock in client" | |
IO.puts :os.system_time(:millisecond) | |
receiver | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment