clientserver

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package clientserver provides the WebSocket server for client applications.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ClientInfo added in v1.1.0

type ClientInfo struct {
	ID          string    `json:"id"`
	Origin      string    `json:"origin,omitempty"`
	RemoteAddr  string    `json:"remoteAddr"`
	UserAgent   string    `json:"userAgent,omitempty"`
	ConnectedAt time.Time `json:"connectedAt"`
	Writes      int       `json:"writes"`
	Locks       int       `json:"locks"`
}

ClientInfo describes a connected client for display.

type Config

type Config struct {
	// APISecret is the API secret required for non-loopback connections.
	// Empty means no auth (legacy / development mode).
	APISecret string

	// AllowedOrigins extends the default same-origin policy. Use ["*"]
	// to disable origin checking entirely (NOT recommended).
	AllowedOrigins []string

	// OriginPolicy, when set, decides origin admission instead of
	// AllowedOrigins and is told about rejections.
	OriginPolicy server.OriginPolicy

	// TokenVerifier recognizes per-device credentials issued at pairing, so a
	// device can be admitted -- and revoked -- independently of the shared
	// secret.
	TokenVerifier server.TokenVerifier

	// OnChange, when set, is called whenever a client connects or disconnects
	// so an observer can refresh without polling. Called off the hot path but
	// on the connection's own goroutine, so it must not block.
	OnChange func()
}

Config holds configuration for the client handling logic. The HTTP listener and TLS are owned by the unified server, so this carries only what the client handlers need.

type Server

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

Server handles client connections for consuming NFC data.

func New

func New(config Config, bridge *server.ServerBridge) *Server

New creates a new client server instance.

func (*Server) ClientCount added in v1.1.0

func (s *Server) ClientCount() int

ClientCount returns the number of currently connected clients.

func (*Server) Clients added in v1.1.0

func (s *Server) Clients() []ClientInfo

Clients returns the connected clients, most recently connected first.

func (*Server) Disconnect added in v1.1.0

func (s *Server) Disconnect(clientID string) bool

Disconnect closes one client's connection by ID, reporting whether it was found. The read loop notices the closed socket and removes the session, so this does not touch the map itself.

func (*Server) GetLastCard

func (s *Server) GetLastCard() *nfc.Card

GetLastCard returns the last received card data.

func (*Server) ServeWS added in v1.1.0

func (s *Server) ServeWS(w http.ResponseWriter, r *http.Request)

ServeWS handles a WebSocket connection request for a client. It performs its own API-secret check and origin validation, so it is safe to call directly from a shared listener (unified single-port mode).

func (*Server) StartBackground added in v1.1.0

func (s *Server) StartBackground(ctx context.Context)

StartBackground starts the client-side background work — the bridge listeners that fan tag data and device status out to connected clients — under the given parent context, without binding an HTTP listener. It returns immediately once the goroutines are running.

The unified server owns the HTTP listener and routes client WebSocket connections here via ServeWS.

func (*Server) Stop

func (s *Server) Stop()

Stop stops the client server's background work. The unified server owns the HTTP listener; this only cancels the context the background goroutines run under.

Jump to

Keyboard shortcuts

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