example of echo server over websockets

This commit is contained in:
Simon Cruanes 2024-02-05 00:28:38 -05:00
parent e1f2edb0ab
commit 7fe66a21ec
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
2 changed files with 57 additions and 0 deletions

View file

@ -20,6 +20,12 @@
(modules writer)
(libraries tiny_httpd logs))
(executable
(name echo_ws)
(flags :standard -warn-error -a+8)
(modules echo_ws)
(libraries tiny_httpd tiny_httpd_ws logs))
(rule
(targets test_output.txt)
(deps

51
examples/echo_ws.ml Normal file
View file

@ -0,0 +1,51 @@
module S = Tiny_httpd
module Log = Tiny_httpd.Log
module IO = Tiny_httpd_io
let setup_logging ~debug () =
Logs.set_reporter @@ Logs.format_reporter ();
Logs.set_level ~all:true
@@ Some
(if debug then
Logs.Debug
else
Logs.Info)
let () =
let port_ = ref 8080 in
let j = ref 32 in
let debug = ref false in
Arg.parse
(Arg.align
[
"--port", Arg.Set_int port_, " set port";
"-p", Arg.Set_int port_, " set port";
"--debug", Arg.Set debug, " enable debug";
"-j", Arg.Set_int j, " maximum number of connections";
])
(fun _ -> raise (Arg.Bad ""))
"echo [option]*";
setup_logging ~debug:!debug ();
let server = S.create ~port:!port_ ~max_connections:!j () in
Tiny_httpd_ws.add_route_handler server
S.Route.(exact "echo" @/ return)
(fun ic oc ->
Log.info (fun k -> k "new client connection");
let buf = Bytes.create 32 in
let continue = ref true in
while !continue do
let n = IO.Input.input ic buf 0 (Bytes.length buf) in
Log.debug (fun k ->
k "echo %d bytes from websocket: %s" n (Bytes.sub_string buf 0 n));
if n = 0 then continue := false;
IO.Output.output oc buf 0 n;
IO.Output.flush oc
done;
Log.info (fun k -> k "client exiting"));
Printf.printf "listening on http://%s:%d\n%!" (S.addr server) (S.port server);
match S.run server with
| Ok () -> ()
| Error e -> raise e