transport

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package transport abstracts the WebSocket layer so the dispatcher never touches a concrete websocket library.

Index

Constants

This section is empty.

Variables

View Source
var ErrFakeClosed = errors.New("transport: fake ws closed")

ErrFakeClosed is returned by a closed FakeWS.

Functions

This section is empty.

Types

type FakeWS

type FakeWS struct {
	// contains filtered or unexported fields
}

FakeWS is an in-memory WS for tests. Inbound messages are injected via Inject; outbound writes are observable via Sent.

func NewFakeWS

func NewFakeWS(subprotocol string) *FakeWS

NewFakeWS creates a fake with the given negotiated subprotocol.

func (*FakeWS) Close

func (f *FakeWS) Close(StatusCode, string) error

func (*FakeWS) Inject

func (f *FakeWS) Inject(msg []byte)

Inject queues an inbound message to be returned by Read.

func (*FakeWS) Ping added in v0.1.2

func (f *FakeWS) Ping(ctx context.Context) error

func (*FakeWS) PingCount added in v0.1.2

func (f *FakeWS) PingCount() int64

PingCount returns how many times Ping has been called.

func (*FakeWS) Read

func (f *FakeWS) Read(ctx context.Context) ([]byte, error)

func (*FakeWS) Sent

func (f *FakeWS) Sent() <-chan []byte

Sent exposes outbound messages written via Write.

func (*FakeWS) SetPingFunc added in v0.1.2

func (f *FakeWS) SetPingFunc(fn func(context.Context) error)

SetPingFunc configures Ping behavior for tests. nil restores the default.

func (*FakeWS) Subprotocol

func (f *FakeWS) Subprotocol() string

func (*FakeWS) Write

func (f *FakeWS) Write(ctx context.Context, data []byte) error

type StatusCode

type StatusCode int

StatusCode mirrors RFC 6455 close codes (subset used by OCPP).

const (
	StatusNormalClosure StatusCode = 1000
	StatusGoingAway     StatusCode = 1001
	StatusProtocolError StatusCode = 1002
	StatusInternalError StatusCode = 1011
)

type WS

type WS interface {
	// Read blocks until one full text message arrives or ctx is cancelled.
	Read(ctx context.Context) ([]byte, error)
	// Write sends one text message, respecting ctx for cancellation.
	Write(ctx context.Context, data []byte) error
	// Ping sends a WebSocket ping and waits for the matching pong.
	Ping(ctx context.Context) error
	// Close sends a close frame with the given status code and reason.
	Close(code StatusCode, reason string) error
	// Subprotocol returns the negotiated subprotocol (e.g. "ocpp1.6").
	Subprotocol() string
}

WS is a context-native WebSocket connection. All blocking operations accept a context so the dispatcher can cancel reads/writes during connection teardown.

func NewCoderWS

func NewCoderWS(c *websocket.Conn) WS

NewCoderWS wraps a coder/websocket connection as a WS.

Jump to

Keyboard shortcuts

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