A ready-made WebSock handler that bridges a
WebSocket connection to a Terminalwire.Server.Session. WebSock is the common
interface spoken by Phoenix, Bandit, and Plug.Cowboy, so this one module wires
Terminalwire into all of them.
You supply a :handler — fun(Terminalwire.Server.Context.t()) — that is your
CLI. Everything between the socket and that function (handshake, framing, flow,
request/response) is handled here.
Plug / Bandit / Cowboy
# in a Plug pipeline
WebSockAdapter.upgrade(conn, Terminalwire.WebSock, [handler: &MyCLI.run/1], [])Phoenix
# in your endpoint/router, upgrade the connection to this handler:
conn
|> WebSockAdapter.upgrade(Terminalwire.WebSock, [handler: &MyCLI.run/1], timeout: :infinity)The handler runs once the client completes the handshake; its integer return value (if any) becomes the exit code.
This module has an optional dependency on
:websock. Add{:websock, "~> 0.5"}(and an adapter like:websock_adapter) to your app to use it.