wsproto

package
v0.0.0-...-f7624ff Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	AbsintheWSSubProtocol = "absinthe"
)
View Source
const (

	// This might seem confusing, but the protocol is called graphql-ws and uses "graphql-transport-ws" as subprotocol
	GraphQLWSSubprotocol = "graphql-transport-ws"
)
View Source
const (

	// Again, this is not a typo. Somehow they managed to give each protocol name to the other's subprotocol identifier.
	SubscriptionsTransportWSSubprotocol = "graphql-ws"
)

Variables

View Source
var (
	CloseKindNormal             = CloseKind{Code: ws.StatusNormalClosure, Reason: "Normal closure"}
	CloseKindGoingAway          = CloseKind{Code: ws.StatusGoingAway, Reason: "Going away"}
	CloseKindUnauthorized       = CloseKind{Code: 4401, Reason: "Unauthorized"}
	CloseKindTooManyInits       = CloseKind{Code: 4429, Reason: "Too many initialisation requests"}
	CloseKindInvalidMessageType = CloseKind{Code: 4400, Reason: "Invalid message type"}
)

Functions

func IsSupportedSubprotocol

func IsSupportedSubprotocol(subProtocol string) bool

func Subprotocols

func Subprotocols() []string

Types

type CloseError

type CloseError struct {
	Err  error
	Kind CloseKind
}

CloseError signals that the protocol layer (or a downstream handler) wants the connection torn down with a specific close kind. The protocol layer does not write the close frame itself — it returns this error so the transport-layer message loop owns the single Close call. Unwraps to the underlying cause for log/debug purposes.

func (*CloseError) Error

func (e *CloseError) Error() string

func (*CloseError) Unwrap

func (e *CloseError) Unwrap() error

type CloseKind

type CloseKind struct {
	Code   ws.StatusCode
	Reason string
}

CloseKind is the WebSocket close code and reason sent to the downstream client when the connection handler tears down. Connection-level concern — never sent from the resolver.

func CloseKindOf

func CloseKindOf(err error) CloseKind

CloseKindOf returns the close kind to use when tearing down a connection after err. Errors that wrap a *CloseError carry their own kind; everything else (network read failure, JSON decode error, etc.) falls back to CloseKindNormal.

type Message

type Message struct {
	ID      string
	Type    MessageType
	Payload json.RawMessage
}

type MessageType

type MessageType int

MessageType indicates the type of the message received from the client

const (
	MessageTypePing MessageType = iota + 1
	MessageTypePong
	MessageTypeSubscribe
	MessageTypeComplete
	MessageTypeTerminate
)

type Proto

type Proto interface {
	Subprotocol() string
	// Initialize starts the protocol and returns the initial payload received from the client.
	// On a protocol-level rejection (bad first message, etc.) it returns a *CloseError carrying
	// the close code and reason. The transport-layer caller writes the close frame.
	Initialize() (json.RawMessage, error)
	// ReadMessage reads the next message from the client. On a protocol-level rejection
	// (duplicate connection_init, unknown message type, etc.) it returns a *CloseError; the
	// transport-layer caller writes the close frame.
	ReadMessage() (*Message, error)

	Pong(*Message) error
	WriteGraphQLData(id string, data json.RawMessage, extensions json.RawMessage) error
	WriteGraphQLErrors(id string, errors json.RawMessage, extensions json.RawMessage) error

	// Complete is sent to indicate the requested operation is done and no more results will come in
	Complete(id string) error
}

func NewProtocol

func NewProtocol(subProtocol string, conn ProtoConn) (Proto, error)

type ProtoConn

type ProtoConn interface {
	ReadJSON(v any) error
	WriteJSON(v any) error
	WriteCloseFrame(code ws.StatusCode, reason string) error
}

Jump to

Keyboard shortcuts

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