ws_session (ws v0.1.1)

View Source

WebSocket session state machine.

Owns a single stream handle (supplied by the embedder through a ws_transport callback), drives the frame codec, auto-responds to control frames, and dispatches inbound messages to the user handler module.

States: open — handshake complete, frames flowing in both directions. closing — a close frame was sent or received; the other direction may still finish. closed — both directions closed; the process is about to exit.

Inbound-byte delivery is pull-based: after consuming a chunk the session calls TransportMod:activate/1 to request the next one. The transport signals fresh bytes with {ws_data, Handle, Bin}, peer close with {ws_closed, Handle}, and socket-level errors with {ws_error, Handle, Reason}.

Summary

Types

start_opts/0

-type start_opts() ::
          #{transport := module(),
            handle := term(),
            role := client | server,
            handler := module(),
            handler_opts => term(),
            req => map(),
            parser_opts => map()}.

Functions

activate(Pid)

-spec activate(pid()) -> ok.

Tell the session the embedder has finished transport setup (ownership transfer and friends) — the session may now read bytes.

callback_mode()

close(Pid, Code)

-spec close(pid(), ws_close:code()) -> ok.

close(Pid, Code, Reason)

-spec close(pid(), ws_close:code(), iodata()) -> ok.

closing(Et, Msg, St)

code_change(OldVsn, State, Data, Extra)

init(Opts)

open(_, Msg, St)

ready_wait(EventType, Msg, St)

send(Pid, FrameOrFrames)

-spec send(pid(), ws_frame:frame() | [ws_frame:frame()]) -> ok.

start(Opts)

-spec start(start_opts()) -> {ok, pid()} | {error, term()}.

start_link(Opts)

-spec start_link(start_opts()) -> {ok, pid()} | {error, term()}.

stop(Pid)

-spec stop(pid()) -> ok.

terminate(Reason, StateName, St)