WebSocket (RFC 6455) over MuroPlug, through the WebSock API.
A Plug upgrades a request with WebSockAdapter.upgrade/4 (what
Phoenix does for sockets and LiveView) or, directly, with
Plug.Conn.upgrade_adapter(conn, :websocket, {MyWebSock, state, opts})The request head was parsed and validated by the book (step.muro) like
any other request. MuroPlug.Adapter.upgrade/3 then checks the
RFC 6455 §4.2.1 opening handshake (check/3 here) and keeps the
WebSock spec on the payload. When the Plug returns,
MuroPlug.Handler writes 101 Switching Protocols (with the response
headers the Plug set, such as set-cookie or sec-websocket-protocol)
and runs MuroPlug.WebSocket.Connection on the same
ThousandIsland.Socket, so ws:// and wss:// are one code path.
A handshake that fails the checks raises
MuroPlug.WebSocket.UpgradeError: 400 Bad Request, or
426 Upgrade Required with sec-websocket-version: 13 for a version
other than 13.
Options (the opts of the upgrade tuple; the WebSockAdapter names):
:timeout— idle milliseconds with no client data before the server closes with 1001 andterminate(:timeout, state). Defaults to60_000.:max_frame_size— the largest frame payload accepted, in octets (or:infinity); a larger frame closes with 1009. Defaults to10_000_000.:max_message_size— the largest assembled (possibly fragmented) message, in octets (or:infinity); larger closes with 1009. Defaults to the:max_frame_sizevalue.:validate_utf8— check that text messages and close reasons are UTF-8 (1007 when not). Uncompressed text is checked as each fragment arrives and as payload bytes of a frame arrive: a prefix that cannot be valid UTF-8 closes before the rest of the frame. Compressed text is checked after inflate. Defaults totrue.:close_timeout— after the server sends a close frame, how long to wait for the client's close before dropping the socket. Defaults to5_000.:fullsweep_after,:max_heap_size— process flags for the connection process. Amax_heap_sizemap must have a non-negative integer:sizeand may have:kill,:error_logger, and:include_shared_binariesbooleans (Erlang'sProcess.flag/2); unknown keys or a negative:sizeraiseArgumentErrorbefore the 101.:compress— negotiate RFC 7692permessage-deflatewhen the client offers it.false(the default) never advertisesSec-WebSocket-Extensions.true, or a keyword list of:zlibdeflate options (:level,:mem_level,:strategy, and:window_bits9..15), accepts a valid offer and compresses outgoing data frames (RSV1). Control frames are never compressed.:deflate_options(or:deflate_opts) is the same keyword list when:compressistrue.:window_bitscaps the server's deflate window and is advertised asserver_max_window_bitswhen it is below 15 or below the client's offer (client_max_window_bitsmay still be 8..15;server_max_window_bits=8is skipped because:zlibcannot deflate with a raw window of 8). Frames with RSV1 set without a negotiated extension, or RSV1 on a continuation or control frame, close with 1002. A corrupt deflate payload or invalid UTF-8 after inflate closes with 1007; inflated bytes past:max_message_sizeclose with 1009.
Known options with bad values (e.g. close_timeout: :infinity)
raise ArgumentError from the upgrade, before the 101; unknown keys
are ignored.
Summary
Functions
Sec-WebSocket-Accept for a client's Sec-WebSocket-Key.
Check an RFC 6455 §4.2.1 opening handshake: GET, HTTP/1.1, a Host,
Upgrade with the websocket token and Connection with the
upgrade token (both case-insensitive, in any header line),
Sec-WebSocket-Version: 13, and one Sec-WebSocket-Key that is
base64 of 16 octets. Returns {:ok, key} or {:error, status, message, headers}.
Functions
Sec-WebSocket-Accept for a client's Sec-WebSocket-Key.
@spec check(binary(), boolean(), [{binary(), binary()}]) :: {:ok, binary()} | {:error, 400 | 426, binary(), [{binary(), binary()}]}
Check an RFC 6455 §4.2.1 opening handshake: GET, HTTP/1.1, a Host,
Upgrade with the websocket token and Connection with the
upgrade token (both case-insensitive, in any header line),
Sec-WebSocket-Version: 13, and one Sec-WebSocket-Key that is
base64 of 16 octets. Returns {:ok, key} or {:error, status, message, headers}.