Skip to content

Instantly share code, notes, and snippets.

@mokele
Created February 3, 2011 18:30
Show Gist options
  • Save mokele/809916 to your computer and use it in GitHub Desktop.
Save mokele/809916 to your computer and use it in GitHub Desktop.
-module(chat).
-compile(export_all).
start() ->
Pid = spawn(fun loop/0),
register(chatroom, Pid),
Pid.
loop() ->
loop([]).
loop(State) ->
io:format("LOOOOOP~n"),
receive
pid ->
io:format("Pid ~p~n", [self()]),
loop(State);
{join, Pid} ->
io:format("Join~n"),
loop([Pid|State]);
{say, _FromPid, Msg} ->
io:format("Say~n"),
say(Msg, State),
loop(State);
{rec, FromPid, Msg} ->
io:format("Rec~n"),
io:format("Pid: ~p said ~p~n", [FromPid, Msg]),
loop(State);
Other ->
io:format("Receive something else: ~p~n", [Other]),
loop(State)
end.
say(_Msg, []) -> ok;
say(Msg, [Pid|T]) ->
io:format("Saying to ~p~n", [Pid]),
Pid ! {rec, self(), Msg},
say(Msg, T).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment