Skip to content

Instantly share code, notes, and snippets.

@wingyplus
Created August 4, 2013 16:03
Show Gist options
  • Save wingyplus/6150816 to your computer and use it in GitHub Desktop.
Save wingyplus/6150816 to your computer and use it in GitHub Desktop.
-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