Created
August 4, 2013 16:03
-
-
Save wingyplus/6150816 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). | |
-export([server/1, client/0]). | |
server(Clients) -> | |
receive | |
{client, Client} -> | |
server(Clients ++ [Client]); | |
{msg, Message} -> | |
send_message(Message, Clients), | |
server(Clients) | |
end. | |
%% client show message when receive. | |
client() -> | |
receive | |
{msg, Message} -> | |
io:format("~p~n", [Message]) | |
end, | |
client(). | |
%% broadcast message to all clients. | |
send_message(_Message, []) -> ok; | |
send_message(Message, [HClient | TClients]) -> | |
HClient ! {msg, Message}, | |
send_message(Message, TClients). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment