我正在尝试执行一个非常简单的Erlang代码,但它不起作用.
我试过没有问题地执行了一些你好的世界,但不是我自己的代码.
-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
最佳答案 您使用c / 1编译代码,但是忘记将其加载到具有l / 1的VM.虽然VM确实自动加载模块(模块尚未加载到VM),但每次编译到新梁时都不会重新加载它们.
如果你在开发中做了很多,你可能想要研究像sync这样的工具.