wsserver

package
v1.6.7 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2026 License: Apache-2.0 Imports: 10 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	ConfigAckTimeout      = "ackTimeout"
	ConfigReadBufferSize  = "readBufferSize"
	ConfigWriteBufferSize = "writeBufferSize"
)

Variables

This section is empty.

Functions

func InitConfig

func InitConfig(conf config.Section)

Types

type BatchHeader

type BatchHeader struct {
	BatchNumber int64  `json:"batchNumber,omitempty"` // In the case of broadcast mode we can omitempty as it's not needed
	Stream      string `json:"stream"`
}

type Protocol

type Protocol interface {
	// Broadcast performs best-effort delivery to all connections currently active on the specified stream
	Broadcast(ctx context.Context, stream string, payload interface{})

	// NextRoundTrip blocks until at least one connection is started on the stream, and then
	// returns an interface that can be used to send a payload to exactly one of the attached
	// connections, and receive an ack/error from just the one connection that was picked.
	// - Returns an error if the context is closed.
	RoundTrip(ctx context.Context, stream string, payload WSBatch) (*WebSocketCommandMessage, error)
}

The Protocol interface layers a protocol on top of raw websockets, that allows the server side to:

  • Model the concept of multiple "streams" on a single WebSocket
  • Block until 1 or more connections are available that have "started" a particular stream
  • Send a broadcast to all connections on a stream
  • Send a single payload to a single selected connection on a stream, and wait for an "ack" back from that specific websocket connection (or that websocket connection to disconnect)

NOTE: This replaces a previous WebSocketChannels interface, which started its life in 2018

and attempted to solve the above problem set in a different way that had a challenging timing issue.

type WSBatch

type WSBatch interface {
	GetBatchHeader() *BatchHeader
}

WSBatch is any serializable structure that contains the batch header

type WebSocketCommandMessage

type WebSocketCommandMessage struct {
	Type        string `json:"type,omitempty"`
	Stream      string `json:"stream,omitempty"` // name of the event stream
	Message     string `json:"message,omitempty"`
	BatchNumber int64  `json:"batchNumber,omitempty"`
}

type WebSocketServer

type WebSocketServer interface {
	Protocol
	Handler(w http.ResponseWriter, r *http.Request)
	Close()
}

WebSocketServer is the full server interface with the init call

func NewWebSocketServer

func NewWebSocketServer(bgCtx context.Context, config *WebSocketServerConfig) WebSocketServer

NewWebSocketServer create a new server with a simplified interface

type WebSocketServerConfig

type WebSocketServerConfig struct {
	AckTimeout      time.Duration
	ReadBufferSize  int64
	WriteBufferSize int64
}

func GenerateConfig

func GenerateConfig(conf config.Section) *WebSocketServerConfig

Jump to

Keyboard shortcuts

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