Chat server: Difference between revisions

m
quick edit to prompt for a different username instead of disconnecting.
(Added Prolog Implementation)
m (quick edit to prompt for a different username instead of disconnecting.)
Line 2,125:
% client can be broadcast to.
handle_connection(Str) :-
send_msg(Str, msg_welcome, []),
repeat,
read_line_to_string(Str, Name),
send_msg(Str, msg_welcome_namemsg_username, Name[]),
connect_user read_line_to_string(NameStr, StrName), !.
connect_user(Name, Str), !.
 
% connections are stored here
Line 2,135 ⟶ 2,136:
connect_user(Name, Str) :-
connected(Name, _),
send_msg(Str, msg_username_taken, []).,
fail.
connect_user(Name, Str) :-
\+ connected(Name, _),
send_msg(Str, msg_welcome_name, Name),
 
% make sure that the connection is removed when the client leaves.
setup_call_cleanup(
Line 2,171 ⟶ 2,175:
 
% constants for the various message types that are sent
msg_welcome('Welcome to Chatalot\n\rPlease Enter your nickname: r').
msg_username('Please enter your nickname: ').
msg_welcome_name('Welcome ~p\n\r').
msg_joined(' -- "~w" has joined the chat --').
msg_left(' -- "~w" has left the chat. --').
msg_username_taken('That username is already taken, choose another\n\r').
msg_new_line('\n\r').
msg_by_user('~w> ~w').</lang>
Anonymous user