Module
whiplash/websocket
src/whiplash/websocket.pookaWebSocket, the conversation that stays open (RFC 6455).
Sans-IO like its siblings: a Reader turns bytes into messages,
reassembling fragments and answering nothing on its own; the frame
writers turn messages into bytes. The handshake helpers check an upgrade
request and compute Sec-WebSocket-Accept, over HTTP/1.1 (RFC 6455
section 4) or an HTTP/2 extended CONNECT (RFC 8441).
permessage-deflate (RFC 7692) is negotiated by negotiate_deflate and
applied by the reader and writer when both ends agreed to it.
API reference
- enumOpcode
- classFrame
- classMessage
- classTextMessage
- classBinaryMessage
- classPingMessage
- classPongMessage
- classCloseMessage
- classWebsocketError
- enumRole
- classDeflate
- classReader
- classUpgrade
- functionreader
- functionwith_deflate
- functionfeed
- functiondecode_frame
- functionencode_frame
- functiontext
- functionbinary
- functionclose
- functionaccept_key
- functioncheck_upgrade
- functioncheck_connect
- functionnegotiate_deflate
- classWriter
- functionwriter
- functionwrite
enum Opcode
The opcodes of RFC 6455 section 5.2.
Opcode.Continuation
Opcode.Text
Opcode.Binary
Opcode.Close
Opcode.Ping
Opcode.Pong
class Frame(fin: Bool, rsv1: Bool, opcode: Opcode, payload: BitArray)
One frame as it is on the wire, after unmasking.
fin: Bool
Boolrsv1: Bool
Boolopcode: Opcode
whiplash/websocket.Opcodepayload: BitArray
BitArray
fn(Bool, Bool, whiplash/websocket.Opcode, BitArray) -> whiplash/websocket.Frameclass Message
A complete message, once its fragments have arrived.
class TextMessage(text: String) is Message
text: String
String
fn(String) -> whiplash/websocket.TextMessageclass BinaryMessage(data: BitArray) is Message
data: BitArray
BitArray
fn(BitArray) -> whiplash/websocket.BinaryMessageclass PingMessage(data: BitArray) is Message
data: BitArray
BitArray
fn(BitArray) -> whiplash/websocket.PingMessageclass PongMessage(data: BitArray) is Message
data: BitArray
BitArray
fn(BitArray) -> whiplash/websocket.PongMessageclass CloseMessage(code: Option(Int), reason: String) is Message
The peer is closing: a code, when it sent one, and a reason.
code: Option(Int)
Option(Int)reason: String
String
fn(Option(Int), String) -> whiplash/websocket.CloseMessageclass WebsocketError(code: Int, message: String)
Why the bytes were refused, with the close code to answer with: 1002 for a protocol error, 1007 for text that is not UTF-8, 1009 for a message too big.
code: Int
Intmessage: String
String
fn(Int, String) -> whiplash/websocket.WebsocketErrorenum Role
Which end of the connection this is. A server requires masked frames from its clients; a client requires unmasked ones.
Role.Server
Role.Client
class Deflate(
server_no_context_takeover: Bool,
client_no_context_takeover: Bool,
server_max_window_bits: Int,
client_max_window_bits: Int
)
permessage-deflate's parameters, as both ends agreed them.
server_no_context_takeover: Bool
Boolclient_no_context_takeover: Bool
Boolserver_max_window_bits: Int
Intclient_max_window_bits: Int
Int
fn(Bool, Bool, Int, Int) -> whiplash/websocket.Deflateclass Reader(
role: Role,
max_message: Int,
deflate: Option(Deflate),
buffer: BitArray,
fragments: List(Frame),
kind: Option(Opcode),
size: Int,
utf8_tail: BitArray,
closed: Bool,
compressed: Bool,
dictionary: BitArray
)
A reader of one connection's frames, with a message half assembled.
role: Role
whiplash/websocket.Rolemax_message: Int
Intdeflate: Option(Deflate)
Option(whiplash/websocket.Deflate)buffer: BitArray
BitArrayfragments: List(Frame)
List(whiplash/websocket.Frame)kind: Option(Opcode)
Option(whiplash/websocket.Opcode)size: Int
Intutf8_tail: BitArray
BitArrayclosed: Bool
Boolcompressed: Bool
Booldictionary: BitArray
BitArray
fn(whiplash/websocket.Role, Int, Option(whiplash/websocket.Deflate), BitArray, List(whiplash/websocket.Frame), Option(whiplash/websocket.Opcode), Int, BitArray, Bool, Bool, BitArray) -> whiplash/websocket.Readerclass Upgrade(key: String, protocols: List(String), extensions: List(String))
What an upgrade request asked for, once checked.
key: String
Stringprotocols: List(String)
List(String)extensions: List(String)
List(String)
fn(String, List(String), List(String)) -> whiplash/websocket.Upgradereader(role: Role, max_message: Int) -> Reader
fn(whiplash/websocket.Role, Int) -> whiplash/websocket.ReaderA reader for one end of a connection, refusing messages over
max_message bytes.
with_deflate(reader: Reader, deflate: Deflate) -> Reader
The same reader, inflating compressed messages.
feed(reader: Reader, bytes: BitArray) -> Result(
#(List(Message), Reader),
WebsocketError
)
fn(whiplash/websocket.Reader, BitArray) -> Result(#(List(whiplash/websocket.Message), whiplash/websocket.Reader), whiplash/websocket.WebsocketError)Feed bytes, and get every message they complete.
decode_frame(role: Role, bytes: BitArray) -> Result(
Option(#(Frame, BitArray)),
WebsocketError
)
fn(whiplash/websocket.Role, BitArray) -> Result(Option(#(whiplash/websocket.Frame, BitArray)), whiplash/websocket.WebsocketError)One frame, from its bytes, and the bytes after it. Ok(None) when more
bytes are needed.
encode_frame(frame: Frame, mask: Option(BitArray)) -> BitArray
fn(whiplash/websocket.Frame, Option(BitArray)) -> BitArrayA frame's bytes. A client passes the four bytes of its mask; a server
passes None.
text(text: String) -> BitArray
fn(String) -> BitArrayA text message as one unmasked frame, as servers send them.
binary(data: BitArray) -> BitArray
fn(BitArray) -> BitArrayA binary message as one unmasked frame.
close(code: Int, reason: String) -> BitArray
fn(Int, String) -> BitArrayA close frame with a code and a reason.
accept_key(key: String) -> String
fn(String) -> StringSec-WebSocket-Accept for a Sec-WebSocket-Key: the key and the GUID
of RFC 6455, hashed with SHA-1, in base64.
check_upgrade(method: String, headers: List(#(String, String))) -> Result(
Upgrade,
WebsocketError
)
fn(String, List(#(String, String))) -> Result(whiplash/websocket.Upgrade, whiplash/websocket.WebsocketError)Check an HTTP/1.1 upgrade request's method and headers (RFC 6455 section 4.2.1).
check_connect(headers: List(#(String, String))) -> Result(
Upgrade,
WebsocketError
)
fn(List(#(String, String))) -> Result(whiplash/websocket.Upgrade, whiplash/websocket.WebsocketError)Check an HTTP/2 extended CONNECT's pseudo-headers and headers (RFC 8441
section 4). There is no key, so Upgrade.key is empty.
negotiate_deflate(offers: List(String)) -> Option(#(Deflate, String))
fn(List(String)) -> Option(#(whiplash/websocket.Deflate, String))Agree to a client's permessage-deflate offer, or not (RFC 7692 section 7.1).
class Writer(role: Role, deflate: Option(Deflate), dictionary: BitArray)
Compression's outgoing dictionary, kept as ordinary bytes. Values do not hide live zlib handles: taking a new Writer never mutates its predecessor.
role: Role
whiplash/websocket.Roledeflate: Option(Deflate)
Option(whiplash/websocket.Deflate)dictionary: BitArray
BitArray
fn(whiplash/websocket.Role, Option(whiplash/websocket.Deflate), BitArray) -> whiplash/websocket.Writerwriter(role: Role, deflate: Option(Deflate)) -> Writer
fn(whiplash/websocket.Role, Option(whiplash/websocket.Deflate)) -> whiplash/websocket.Writerwrite(writer: Writer, message: Message, mask: Option(BitArray)) -> Result(
#(BitArray, Writer),
WebsocketError
)
fn(whiplash/websocket.Writer, whiplash/websocket.Message, Option(BitArray)) -> Result(#(BitArray, whiplash/websocket.Writer), whiplash/websocket.WebsocketError)Encode a message and return the writer for the next one. A client supplies a fresh unpredictable four-byte mask for each call; a server supplies None.
let state = websocket.writer(websocket.Role.Server, None)
let assert Ok(#(bytes, _)) = websocket.write(state, TextMessage("hello"), None)