Module Websocket_lwt_unix

This module implements a websocket client and server library in the spirit of the otherwise similar TCP functions of the Lwt_io module. The websocket protocol add message framing in addition of simple TCP communication, and this library implement framing and unframing of messages.

include Websocket.S with type 'a IO.t := 'a Cohttp_lwt_unix.IO.t and type IO.ic := Cohttp_lwt_unix.IO.ic and type IO.oc := Cohttp_lwt_unix.IO.oc
module IO : Cohttp.S.IO with type 'a t := 'a Cohttp_lwt_unix.IO.t with type ic := Cohttp_lwt_unix.IO.ic with type oc := Cohttp_lwt_unix.IO.oc
type mode =
  1. | Client of int -> string
  2. | Server
val make_read_frame : ?buf:Stdlib.Buffer.t -> mode:mode -> IO.ic -> IO.oc -> unit -> Websocket.Frame.t IO.t
val write_frame_to_buf : mode:mode -> Stdlib.Buffer.t -> Websocket.Frame.t -> unit
module Request : Cohttp.S.Http_io with type t = Cohttp.Request.t and type 'a IO.t = 'a IO.t and type IO.ic = IO.ic and type IO.oc = IO.oc
module Response : Cohttp.S.Http_io with type t = Cohttp.Response.t and type 'a IO.t = 'a IO.t and type IO.ic = IO.ic and type IO.oc = IO.oc
module Connected_client : sig ... end
type conn
val read : conn -> Websocket.Frame.t Lwt.t
val write : conn -> Websocket.Frame.t -> unit Lwt.t
val close_transport : conn -> unit Lwt.t

close_transport conn closes the underlying transport. You have to manage the connection state (ie. send close frames, etc.) yourself.

val connect : ?extra_headers:Cohttp.Header.t -> ?random_string:(int -> string) -> ?ctx:Conduit_lwt_unix.ctx -> ?buf:Stdlib.Buffer.t -> Conduit_lwt_unix.client -> Uri.t -> conn Lwt.t
val establish_server : ?read_buf:Stdlib.Buffer.t -> ?write_buf:Stdlib.Buffer.t -> ?timeout:int -> ?stop:unit Lwt.t -> ?on_exn:(exn -> unit) -> ?check_request:(Cohttp.Request.t -> bool) -> ?ctx:Conduit_lwt_unix.ctx -> mode:Conduit_lwt_unix.server -> (Connected_client.t -> unit Lwt.t) -> unit Lwt.t

exception_handler defaults to Lwt.async_exception_hook check_request is called before the http upgrade. If it returns false, the websocket connection is aborted with a "403 Forbidden" response. It defaults to check_origin_with_host

Convenience functions

val mk_frame_stream : (unit -> Websocket.Frame.t Lwt.t) -> Websocket.Frame.t Lwt_stream.t

mk_frame_stream f is a stream build from f, which role is to receive the frames that will form the stream. When a Close frame is received, the stream will be closed.

val establish_standard_server : ?read_buf:Stdlib.Buffer.t -> ?write_buf:Stdlib.Buffer.t -> ?timeout:int -> ?stop:unit Lwt.t -> ?on_exn:(exn -> unit) -> ?check_request:(Cohttp.Request.t -> bool) -> ?ctx:Conduit_lwt_unix.ctx -> mode:Conduit_lwt_unix.server -> (Connected_client.t -> unit Lwt.t) -> unit Lwt.t

establish_standard_server is like establish_server but with automatic handling of some frames:

  • A Pong frame is sent in response to a Ping frame,
  • a Close frame is sent in response to a Close frame.

All frames are then passed to the frame handling function.