tt

package module
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2025 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package tt provides components for writing mqtt-v5 clients.

Example (Client)

Example shows a simple client for connect, publish a QoS 0 and disconnect.

package main

import (
	"context"

	"sogvin.com/mq"
	"sogvin.com/tt"
	"sogvin.com/tt/event"
)

func main() {
	client := tt.NewClient()
	client.SetServer("tcp://localhost:1883")

	ctx := context.Background()
	go client.Run(ctx)

	// v is either an packet or a event type
	for v := range client.Events() {
		switch v := v.(type) {
		case event.ClientUp:
			_ = client.Send(ctx, mq.NewConnect())

		case event.ClientConnect:
			// do something once you are connected
			p := mq.Pub(0, "gopher/happy", "yes")
			_ = client.Send(ctx, p)

		case *mq.Publish:
			_ = v // do something the received packet

		case event.ClientStop:
			// do some clean up maybe
		}
	}
}
Example (Server)

Example shows how to run the provided server.

package main

import (
	"context"
	"log"

	"sogvin.com/tt"
	"sogvin.com/tt/event"
)

func main() {
	srv := tt.NewServer()
	ctx := context.Background()
	go srv.Run(ctx)

	for v := range srv.Events() {
		switch v := v.(type) {
		case event.ServerStop:
			if v.Err != nil {
				log.Println(v.Err)
			}
		}
	}
}

Index

Examples

Constants

View Source
const (
	KB = float64(1024)
	MB = KB * KB
)
View Source
const StatLine = "Revision ConnCount ConnActive NumGoroutine MemAlloc"

Variables

View Source
var ErrClientStopped = fmt.Errorf("Client stopped")
View Source
var ErrIDPoolEmpty = fmt.Errorf("no available packet ids")
View Source
var ErrMalformedTopicName = fmt.Errorf("malformed topic name")

Functions

This section is empty.

Types

type Bind

type Bind struct {
	// eg. tcp://localhost[:port]
	URL string

	// eg. 500ms
	AcceptTimeout string
}

Bind holds server listening settings

type Client

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

Client implements a mqtt-v5 client.

func NewClient

func NewClient() *Client

NewClient returns new client with no logging pointing to tcp://127.0.0.1:1883 wich max packet ID 10.

func (*Client) Events

func (c *Client) Events() <-chan interface{}

Events returns a channel used by client to inform application layer of packets and events. E.g. event.ClientUp

func (*Client) Run

func (c *Client) Run(ctx context.Context) error

Run the client, blocks until stopped or disconnected.

func (*Client) Send

func (c *Client) Send(ctx context.Context, p mq.Packet) error

Send returns when the packet was successfully encoded on the wire. Returns ErrClientStopped if not running. Send is safe to call concurrently.

func (*Client) SetDebug

func (c *Client) SetDebug(v bool)

func (*Client) SetLogger

func (c *Client) SetLogger(v *log.Logger)

func (*Client) SetMaxPacketID

func (c *Client) SetMaxPacketID(v uint16)

func (*Client) SetServer

func (c *Client) SetServer(v string)

type Connection

type Connection interface {
	io.ReadWriteCloser
	SetReadDeadline(time.Time) error
	RemoteAddr() net.Addr
}

type Server

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

func NewServer

func NewServer() *Server

NewServer returns a server ready to run. Configure any settings before calling Run.

func (*Server) AddBind

func (s *Server) AddBind(b *Bind)

AddBind which to listen on for connections, defaults to tcp://localhost:, ie. random port on localhost.

func (*Server) ConnectTimeout

func (s *Server) ConnectTimeout() time.Duration

func (*Server) Events

func (s *Server) Events() <-chan interface{}

Events returns a channel used by server to inform the application layer of events. E.g event.ServerStop

func (*Server) Handle

func (s *Server) Handle(ctx context.Context, conn Connection)

func (*Server) Incoming

func (s *Server) Incoming() chan<- Connection

Incoming returns channel on which to feed new connections

func (*Server) Run

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

Run the server. Use Server.Events to listen for progress.

func (*Server) SetConnectTimeout

func (s *Server) SetConnectTimeout(v time.Duration)

Timeout for the initial connect packet from a client before disconnecting, default 200ms.

func (*Server) SetDebug

func (s *Server) SetDebug(v bool)

SetDebug increases log information, default false.

func (*Server) SetLogger

func (s *Server) SetLogger(v *log.Logger)

SetLogger to use for this server, defaults to no logging.

Source Files

  • client.go
  • match.go
  • router.go
  • serve.go
  • server.go
  • tt.go

Directories

Path Synopsis
Package arn provides an MQTT topic filter matcher.
Package arn provides an MQTT topic filter matcher.
cmd
plot command
tt command
Command tt is a mqtt pub/sub client and broker
Command tt is a mqtt pub/sub client and broker
Command feature documents tt features
Command feature documents tt features
Package event provides client and server event types.
Package event provides client and server event types.
Package spec implements implementation verifiers against the MQTTv5 specification [4.7 Topic names and filters]: https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901241
Package spec implements implementation verifiers against the MQTTv5 specification [4.7 Topic names and filters]: https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901241
Package ttx provides test types
Package ttx provides test types

Jump to

Keyboard shortcuts

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