我正在尝试执行一个非常简单的 Erlang 代码,但它不起作用。我已经尝试执行了一些没有问题的 hello world,但不是我自己的代码。
-module(server).
%% Exported Functions
-export([start/0, process_requests/1]).
%% API Functions
start() ->
ServerPid = spawn(server, process_requests, [[]]),
register(myserver, ServerPid).
process_requests(Clients) ->
receive
{client_join_req, Name, From} ->
NewClients = [From|Clients], %% TODO: COMPLETE
broadcast(NewClients, {join, Name}),
process_requests(NewClients); %% TODO: COMPLETE
{client_leave_req, Name, From} ->
NewClients = lists:delete(From, Clients), %% TODO: COMPLETE
broadcast(Clients, {leave, Name}), %% TODO: COMPLETE
process_requests(NewClients); %% TODO: COMPLETE
{send, Name, Text} ->
broadcast(Clients, {message, Name, Text}), %% TODO: COMPLETE
process_requests(Clients)
end.
%% Local Functions
broadcast(PeerList, Message) ->
Fun = fun(Peer) -> Peer ! Message end,
lists:map(Fun, PeerList).
编译结果:
5> c(server).
{ok,server}
6> server:start().
** exception error: undefined function server:start/0