-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #78 from c-cube/wip-ws
add a websocket library
- Loading branch information
Showing
17 changed files
with
1,059 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ name: github pages | |
on: | ||
push: | ||
branches: | ||
- master | ||
- main | ||
|
||
jobs: | ||
deploy: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/bin/sh | ||
exec dune exec --display=quiet --profile=release "examples/echo_ws.exe" -- $@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
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 handle_ws _client_addr ic oc = | ||
Log.info (fun k -> | ||
k "new client connection from %s" | ||
(Tiny_httpd_util.show_sockaddr _client_addr)); | ||
|
||
let (_ : Thread.t) = | ||
Thread.create | ||
(fun () -> | ||
while true do | ||
Thread.delay 3.; | ||
IO.Output.output_string oc "(special ping!)"; | ||
IO.Output.flush oc | ||
done) | ||
() | ||
in | ||
|
||
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") | ||
|
||
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) | ||
handle_ws; | ||
|
||
Printf.printf "listening on http://%s:%d\n%!" (S.addr server) (S.port server); | ||
match S.run server with | ||
| Ok () -> () | ||
| Error e -> raise e |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.