structure Room = Broadcast.Make(struct
type t = string
end)
sequence s
table t : { Id : int, Title : string, Room : Room.topic }
PRIMARY KEY Id
fun chat id () =
r <- oneRow (SELECT t.Title, t.Room FROM t WHERE t.Id = {[id]});
ch <- Room.subscribe r.T.Room;
newLine <- source "";
buf <- Buffer.create;
let
fun onload () =
let
fun listener () =
s <- recv ch;
Buffer.write buf s;
listener ()
in
listener ()
end
fun getRoom () =
r <- oneRow (SELECT t.Room FROM t WHERE t.Id = {[id]});
return r.T.Room
fun speak line =
room <- getRoom ();
Room.send room line
fun doSpeak () =
line <- get newLine;
set newLine "";
rpc (speak line)
in
return
{[r.T.Title]}
end
fun list () =
queryX' (SELECT * FROM t)
(fn r =>
count <- Room.subscribers r.T.Room;
return
{[r.T.Id]}
{[r.T.Title]}
{[count]}
)
and delete id () =
dml (DELETE FROM t WHERE Id = {[id]});
main ()
and main () =
let
fun create r =
id <- nextval s;
room <- Room.create;
dml (INSERT INTO t (Id, Title, Room) VALUES ({[id]}, {[r.Title]}, {[room]}));
main ()
in
ls <- list ();
return