Created
February 3, 2011 18:30
-
-
Save mokele/809916 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
-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