package inuit

  1. Overview
  2. Docs
type 'a controller

A message Socket.controller is a bidirectional stream of message. The creator of a value of this type can:

  • be notified when connection is established or lost,
  • be notified when a value of type message is received,
  • send messages to the other end,
  • terminates the connection.
val make : receive:('a -> unit) -> 'a controller

make ~receive creates a new controller that will call receive when a message is received.

val set_receive : 'a controller -> ('a -> unit) -> unit

set_receive sets the callback invoked when a message is received.

set_on_closed sets the callback invoked when the connection terminates. Closing is definitive (it can happen at most once).

val set_on_closed : 'a controller -> (unit -> unit) -> unit

set_on_closed sets the callback invoked when the connection terminates. Closing is definitive (it can happen at most once).

set_on_connected sets the callback invoked when the connection is established. It can be invoked at most once.

val set_on_connected : 'a controller -> (unit -> unit) -> unit

set_on_connected sets the callback invoked when the connection is established. It can be invoked at most once.

val send : 'msg controller -> 'msg -> unit

send ctrl msg sends one message to the other end. status (endpoint ctrl) should be `Connected for this to succeed. Fail with Invalid_argument otherwise.

val close : 'msg controller -> unit

close ctrl terminates the connection now. If status (endpoint ctrl) is `Pending or `Connected, it is updated to `Closed and on_closed callback is invoked. If status (endpoint ctrl) is already `Closed, nothing happens.

type 'a t

A handle exposed to higher-level code to connect sockets together.

val endpoint : 'a controller -> 'a t
val status : 'msg t -> [ `Pending | `Connected | `Closed ]

Get the status of the socket. Possible statuses are:

  • `Pending, connection has not been established yet, no message should be sent yet.
  • `Connected, connection has been established, messages can be sent.
  • `Closed, connection is terminated, no messages can be sent anymore.
val connect : a:'msg t -> b:'msg t -> unit

Connect two sockets together. Both should be in `Pending status. Fail with Invalid_argument otherwise.