mirror of
https://github.com/c-cube/tiny_httpd.git
synced 2025-12-06 11:15:35 -05:00
fix websocket
This commit is contained in:
parent
0d750cd86c
commit
22f158ccd8
4 changed files with 25 additions and 28 deletions
|
|
@ -3,9 +3,10 @@
|
||||||
(name tiny_httpd_ws)
|
(name tiny_httpd_ws)
|
||||||
(public_name tiny_httpd.ws)
|
(public_name tiny_httpd.ws)
|
||||||
(synopsis "Websockets for tiny_httpd")
|
(synopsis "Websockets for tiny_httpd")
|
||||||
(private_modules common_ utils_)
|
(private_modules common_ws_ utils_)
|
||||||
|
(flags :standard -open Tiny_httpd_core)
|
||||||
(foreign_stubs
|
(foreign_stubs
|
||||||
(language c)
|
(language c)
|
||||||
(names tiny_httpd_ws_stubs)
|
(names tiny_httpd_ws_stubs)
|
||||||
(flags :standard -std=c99 -fPIC -O2))
|
(flags :standard -std=c99 -fPIC -O2))
|
||||||
(libraries tiny_httpd threads))
|
(libraries tiny_httpd.core threads))
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,4 @@
|
||||||
open Common_
|
open Common_ws_
|
||||||
open Tiny_httpd_server
|
|
||||||
module Log = Tiny_httpd_log
|
|
||||||
module IO = Tiny_httpd_io
|
|
||||||
|
|
||||||
type handler = Unix.sockaddr -> IO.Input.t -> IO.Output.t -> unit
|
type handler = Unix.sockaddr -> IO.Input.t -> IO.Output.t -> unit
|
||||||
|
|
||||||
|
|
@ -382,21 +379,23 @@ let upgrade ic oc : _ * _ =
|
||||||
let writer = Writer.create ~oc () in
|
let writer = Writer.create ~oc () in
|
||||||
let reader = Reader.create ~ic ~writer () in
|
let reader = Reader.create ~ic ~writer () in
|
||||||
let ws_ic : IO.Input.t =
|
let ws_ic : IO.Input.t =
|
||||||
{
|
object
|
||||||
input = (fun buf i len -> Reader.read reader buf i len);
|
inherit IO.Input.t_from_refill ()
|
||||||
close = (fun () -> Reader.close reader);
|
|
||||||
}
|
method private refill (slice : IO.Slice.t) =
|
||||||
|
slice.off <- 0;
|
||||||
|
slice.len <- Reader.read reader slice.bytes 0 (Bytes.length slice.bytes)
|
||||||
|
|
||||||
|
method! close () = Reader.close reader
|
||||||
|
end
|
||||||
in
|
in
|
||||||
let ws_oc : IO.Output.t =
|
let ws_oc : IO.Output.t =
|
||||||
{
|
object
|
||||||
flush =
|
method close () = Writer.close writer
|
||||||
(fun () ->
|
method flush () = Writer.flush writer
|
||||||
Writer.flush writer;
|
method output bs i len = Writer.output writer bs i len
|
||||||
IO.Output.flush oc);
|
method output_char c = Writer.output_char writer c
|
||||||
output_char = Writer.output_char writer;
|
end
|
||||||
output = Writer.output writer;
|
|
||||||
close = (fun () -> Writer.close writer);
|
|
||||||
}
|
|
||||||
in
|
in
|
||||||
ws_ic, ws_oc
|
ws_ic, ws_oc
|
||||||
|
|
||||||
|
|
@ -404,7 +403,7 @@ let upgrade ic oc : _ * _ =
|
||||||
module Make_upgrade_handler (X : sig
|
module Make_upgrade_handler (X : sig
|
||||||
val accept_ws_protocol : string -> bool
|
val accept_ws_protocol : string -> bool
|
||||||
val handler : handler
|
val handler : handler
|
||||||
end) : UPGRADE_HANDLER = struct
|
end) : Server.UPGRADE_HANDLER = struct
|
||||||
type handshake_state = unit
|
type handshake_state = unit
|
||||||
|
|
||||||
let name = "websocket"
|
let name = "websocket"
|
||||||
|
|
@ -454,10 +453,10 @@ end) : UPGRADE_HANDLER = struct
|
||||||
end
|
end
|
||||||
|
|
||||||
let add_route_handler ?accept ?(accept_ws_protocol = fun _ -> true)
|
let add_route_handler ?accept ?(accept_ws_protocol = fun _ -> true)
|
||||||
(server : Tiny_httpd_server.t) route (f : handler) : unit =
|
(server : Server.t) route (f : handler) : unit =
|
||||||
let module M = Make_upgrade_handler (struct
|
let module M = Make_upgrade_handler (struct
|
||||||
let handler = f
|
let handler = f
|
||||||
let accept_ws_protocol = accept_ws_protocol
|
let accept_ws_protocol = accept_ws_protocol
|
||||||
end) in
|
end) in
|
||||||
let up : upgrade_handler = (module M) in
|
let up : Server.upgrade_handler = (module M) in
|
||||||
Tiny_httpd_server.add_upgrade_handler ?accept server route up
|
Server.add_upgrade_handler ?accept server route up
|
||||||
|
|
|
||||||
|
|
@ -4,9 +4,6 @@
|
||||||
for a websocket server. It has no additional dependencies.
|
for a websocket server. It has no additional dependencies.
|
||||||
*)
|
*)
|
||||||
|
|
||||||
open Tiny_httpd_server
|
|
||||||
module IO = Tiny_httpd_io
|
|
||||||
|
|
||||||
type handler = Unix.sockaddr -> IO.Input.t -> IO.Output.t -> unit
|
type handler = Unix.sockaddr -> IO.Input.t -> IO.Output.t -> unit
|
||||||
(** Websocket handler *)
|
(** Websocket handler *)
|
||||||
|
|
||||||
|
|
@ -16,8 +13,8 @@ val upgrade : IO.Input.t -> IO.Output.t -> IO.Input.t * IO.Output.t
|
||||||
val add_route_handler :
|
val add_route_handler :
|
||||||
?accept:(unit Request.t -> (unit, int * string) result) ->
|
?accept:(unit Request.t -> (unit, int * string) result) ->
|
||||||
?accept_ws_protocol:(string -> bool) ->
|
?accept_ws_protocol:(string -> bool) ->
|
||||||
Tiny_httpd_server.t ->
|
Server.t ->
|
||||||
(upgrade_handler, upgrade_handler) Route.t ->
|
(Server.upgrade_handler, Server.upgrade_handler) Route.t ->
|
||||||
handler ->
|
handler ->
|
||||||
unit
|
unit
|
||||||
(** Add a route handler for a websocket endpoint.
|
(** Add a route handler for a websocket endpoint.
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue