socket

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2026 License: MIT Imports: 11 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) *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 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 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