wire

package
v0.500.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 1, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package wire formats and parses the line-based JaWS WebSocket protocol.

Each message is encoded as What<TAB>Jid<TAB>Data<LF>. Data for most commands is written by WsMsg.Append as a JSON-compatible quoted string (see appendJSONQuote) so the browser can decode it with JSON.parse; the server decodes it with Parse (strconv.Unquote, whose grammar is a superset). The Set and Call commands carry path/function payloads directly, so callers must keep those payloads free of raw tabs and newlines; jaws.JsCall compacts JSON for that reason before it enters the wire layer.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendJSONQuote added in v0.500.0

func AppendJSONQuote(b []byte, s string) []byte

AppendJSONQuote appends s to b as a double-quoted JSON string literal that the browser's JSON.parse accepts. Use it instead of strconv.AppendQuote when the quoted data is written into a WebSocket frame: strconv emits Go-only escapes (\xNN, \UXXXXXXXX) for control bytes, DEL and invalid UTF-8 that JSON.parse rejects. See appendJSONQuote for the exact behavior, which is pinned by Fuzz_appendJSONQuote.

func PingLoop added in v0.300.2

func PingLoop(ctx context.Context, ccf context.CancelCauseFunc, doneCh <-chan struct{}, interval, timeout time.Duration, ws *websocket.Conn)

PingLoop sends periodic WebSocket pings and reports ping errors through ccf.

Returns immediately when interval is non-positive.

func ReadLoop

func ReadLoop(ctx context.Context, ccf context.CancelCauseFunc, doneCh <-chan struct{}, incomingMsgCh chan<- WsMsg, ws *websocket.Conn)

ReadLoop reads WebSocket text messages, parses them, and sends parsed messages on incomingMsgCh.

Closes incomingMsgCh on exit.

func WriteLoop

func WriteLoop(ctx context.Context, ccf context.CancelCauseFunc, doneCh <-chan struct{}, outboundMsgCh <-chan WsMsg, ws *websocket.Conn)

WriteLoop reads messages from outboundMsgCh, formats them, and writes them to the WebSocket.

Closes the WebSocket on exit.

Types

type Message

type Message struct {
	Dest any       // destination tag, HTML ID or *jaws.Element
	What what.What // command to perform
	Data string    // payload, such as inner HTML content or a slice of tags
}

Message contains the elements of a message to be sent to requests.

func (*Message) String

func (msg *Message) String() string

String returns the Message in a form suitable for debug output.

type WsMsg

type WsMsg struct {
	Data string    // data to send
	Jid  jid.Jid   // Jid to send, or -1 if Data contains that already
	What what.What // command
}

WsMsg is a message sent to or from a WebSocket.

func Parse

func Parse(txt []byte) (WsMsg, bool)

Parse parses an incoming text buffer into a message.

The wire format mirrors WsMsg.Append. For commands other than what.Set and what.Call, if the Data field begins with a double quote it is decoded with strconv.Unquote and the message is rejected if that fails; data that does not begin with a double quote is taken verbatim. Set and Call data is always taken verbatim. In all cases the resulting data is sanitized with strings.ToValidUTF8.

func (*WsMsg) Append

func (m *WsMsg) Append(b []byte) []byte

Append appends m in wire format to b and returns the extended buffer.

func (*WsMsg) FillAlert

func (m *WsMsg) FillAlert(err error)

FillAlert replaces m with an escaped danger alert for err.

func (*WsMsg) Format

func (m *WsMsg) Format() string

Format returns m in wire format.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL