Membrane.WebRTC.Server.Peer behaviour (WebRTC Server v0.1.1) View Source
Module that manages websocket lifecycle and communication with client.
Adapts the
:cowboy_websocket
behaviour.
Link to this section Summary
Types
Defines custom state of a peer, passed as argument and returned by callbacks.
Defines unique indetifier (UUIDv4) bound to peer.
Callbacks
Callback invoked when a peer process is started.
Callback invoked after successful decoding received JSON message.
Callback invoked when a peer is shutting down.
Callback invoked to extract credentials and metadata from request.
Link to this section Types
Link to this section Functions
Specs
send_to_client(peer :: pid(), message :: Membrane.WebRTC.Server.Message.t()) :: :ok
Encodes message into JSON and sends it to client.
Specs
stop(peer :: pid()) :: :ok
Stops peer process.
Link to this section Callbacks
Specs
on_init( context :: Membrane.WebRTC.Server.Peer.Context.t(), auth_data :: Membrane.WebRTC.Server.Peer.AuthData.t(), options :: any() ) :: {:ok, state :: internal_state()} | {:ok, websocket_options :: :cowboy_websocket.opts(), state :: internal_state()} | {:error, cause :: any()}
Callback invoked when a peer process is started.
Useful both for confirming the identity of the client, as well as setting up state and/or custom Cowboy WebSocket options, like timeout or maximal frame size.
Options argument is value given under the :custom_options
field in
Membrane.WebRTC.Server.Options
.
Returning {:ok, websocket_options, state}
will set up WebSocket options to the ones returned.
Returning {:ok, state}
will set up default WebSocket options with 15
minutes timeout.
Returning {:error, details}
will abort initialization of WebSocket
and return a response with status 401.
Specs
on_receive( message :: Membrane.WebRTC.Server.Message.t(), context :: Membrane.WebRTC.Server.Peer.Context.t(), state :: internal_state() ) :: {:ok, message :: Membrane.WebRTC.Server.Message.t(), state :: internal_state()} | {:ok, state :: internal_state()}
Callback invoked after successful decoding received JSON message.
Peer will proceed to send message returned by this callback to room,
ergo returning {:ok, state}
will cause ignoring message.
Useful for modifying or ignoring messages.
Specs
on_terminate( context :: Membrane.WebRTC.Server.Peer.Context.t(), state :: internal_state() ) :: :ok
Callback invoked when a peer is shutting down.
Useful for any cleanup required.
Specs
parse_request(request :: :cowboy_req.req()) :: {:ok, credentials :: map(), metadata :: any(), room_name :: String.t()} | {:error, cause :: any()}
Callback invoked to extract credentials and metadata from request.
After successfully parsing the request, {:ok, credentials, metadata, room_name}
should be
returned. Credentials and metadata will be used to create Membrane.WebRTC.Server.Peer.AuthData
which is passed to on_init/3
and Membrane.WebRTC.Server.Room.on_join/2
.
Returning {:error, details}
will abort initialization of WebSocket
and return a response with status 400.
Peer will later try to join the room registered under room_name
.
If no such room can be found, peer will abort initialization of WebSocket
and return a response with status 404.