Created
February 9, 2016 01:20
-
-
Save sjahandideh/fa619f98966ae077f1fa to your computer and use it in GitHub Desktop.
sample elixir server client
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 Server do | |
def start(callback_module) do | |
spawn fn -> | |
callback_module |> run | |
end | |
end | |
def run(callback_module) do | |
receive do | |
message -> | |
message |> callback_module.handle_message | |
callback_module |> run | |
end | |
end | |
end | |
defmodule Client do | |
def handle_message("ping"), do: IO.puts("pong") | |
def handle_message("pong"), do: IO.puts("ping") | |
def handle_message(_other), do: IO.puts("Not playing this game! ¯\_(ツ)_/¯") | |
end | |
# server = Server.start Client | |
# send server, "ping" -> returns "pong" | |
# send server, "pong" -> returns "ping" | |
# send server, "blah" -> returns "Not playing this game! ¯\_(ツ)_/¯" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment