Help us shape the future of Go! Take the 2026 Go Developer Survey

socket

package
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package socket provides a server and client for creating and using sessions over a socket.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client is a low-level client for reading and writing to a socket.

func NewClient

func NewClient(address string, options ...ClientOption) *Client

NewClient creates and returns a new client configured with the provided address.

func (*Client) Address

func (c *Client) Address() string

Address returns the address for the client.

func (*Client) Close

func (c *Client) Close() error

Close the connection to the server.

func (*Client) Conn

func (c *Client) Conn() net.Conn

Conn returns the connection of the client.

func (*Client) Stream

func (c *Client) Stream(ctx context.Context, b []byte) (io.ReadCloser, error)

Stream performs a request to the socket server with the provided data and returns an io.ReadCloser to enable reading of the incoming stream of responses.

type ClientOption added in v0.8.0

type ClientOption func(*Client)

func WithTCPDialer added in v0.8.0

func WithTCPDialer() ClientOption

WithTCPDialer sets the dialer to use 'tcp' instead of 'unix'.

type Dispatcher added in v0.6.0

type Dispatcher struct {
	NotFound Handler
	// contains filtered or unexported fields
}

Dispatcher is a Handler that dispatches requests based on the type byte set in the beginning of the incoming payload.

func NewDispatcher added in v0.6.0

func NewDispatcher() *Dispatcher

NewDispatcher creates and return a new dispatcher.

func (*Dispatcher) Handle added in v0.6.0

func (d *Dispatcher) Handle(t byte, h Handler)

Handle registers the handler for the given type byte in the dispatcher.

func (*Dispatcher) HandleFunc added in v0.6.0

func (d *Dispatcher) HandleFunc(t byte, f func(context.Context, Socket))

HandleFunc registers the handler for the given type byte in the dispatcher.

func (*Dispatcher) ServeSocket added in v0.6.0

func (d *Dispatcher) ServeSocket(ctx context.Context, s Socket)

ServeSocket reads the leading type byte and calls the handler with the matching type byte.

type Handler

type Handler interface {
	ServeSocket(ctx context.Context, s Socket)
}

Handler respons to socket requests.

type HandlerFunc

type HandlerFunc func(ctx context.Context, s Socket)

HandlerFunc is an adapter to allow the use of ordinary functions as socket handlers.

func (HandlerFunc) ServeSocket

func (f HandlerFunc) ServeSocket(ctx context.Context, s Socket)

ServeSocket calls f(ctx, s)

type Server

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

func NewServer

func NewServer(address string, handler Handler, options ...ServerOption) *Server

NewServer creates and returns a new socket server.

func (*Server) Run

func (s *Server) Run() (err error)

Run the socket server and accept and handle connections.

func (*Server) Shutdown

func (s *Server) Shutdown(ctx context.Context) error

Shutdown the socket server, close all connections and release all resources.

type ServerOption

type ServerOption func(*Server)

func WithLogger

func WithLogger(logger *slog.Logger) ServerOption

WithLogger sets logger on the server.

func WithTCPListener added in v0.8.0

func WithTCPListener() ServerOption

WithTCPListener sets the listener to use 'tcp' instead of 'unix'.

func WithTimeout

func WithTimeout(timeout time.Duration) ServerOption

WithTimeout sets the timeout for all connections.

type Socket

type Socket interface {
	io.ReadWriter

	// Deadline returns the configured deadline
	// on the socket.
	Deadline() time.Time
	// SetDeadline sets the read and write deadlines associated with the connection.
	// It is equivalent to calling both SetReadDeadline and SetWriteDeadline.
	SetDeadline(t time.Time) error
}

Socket wraps around io.ReadWriter and SetDeadline. The internal handler is responsible for closing the socket (connection).

Jump to

Keyboard shortcuts

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