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" "github.com/gregoryv/mq" "github.com/gregoryv/tt" "github.com/gregoryv/tt/event" ) func main() { client := tt.Client{ Server: "tcp://localhost:1883", } ctx := context.Background() client.Start(ctx) // v is either an packet or a event type for v := range client.Signal() { 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 } } }
Output:
Example (Server) ¶
Example shows how to run the provided server.
package main import ( "context" "github.com/gregoryv/tt" "github.com/gregoryv/tt/event" ) func main() { var srv tt.Server ctx, cancel := context.WithCancel(context.Background()) srv.Start(ctx) for v := range srv.Signal() { switch v.(type) { case event.ServerStop: cancel() } } }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ErrClientStopped = fmt.Errorf("Client stopped")
View Source
var ErrIDPoolEmpty = fmt.Errorf("no available packet ids")
Functions ¶
This section is empty.
Types ¶
type Client ¶ added in v0.9.0
type Client struct { // Server to connect to, defaults to tcp://localhost:1883 Server string // Set to true to include more log output Debug bool // optional logger, leave empty for no logging *log.Logger `json:-` // Outgoing packets use ids from 1..MaxPacketID, this limits the // number of packets in flight. MaxPacketID uint16 // show settings on client.Run ShowSettings bool // contains filtered or unexported fields }
Client implements a mqtt-v5 client. Public fields should be set before calling Run.
func (*Client) Send ¶ added in v0.9.0
Send returns when the packet was successfully encoded on the wire. Returns ErrClientStopped if not running.
func (*Client) Signal ¶ added in v0.10.0
func (c *Client) Signal() <-chan interface{}
Signal returns a channel used by client to inform application layer of packets and events. E.g. event.ClientUp
type Server ¶ added in v0.2.0
type Server struct { // bind configuration where server listens for connections, empty // defaults to random port on localhost Binds []*Bind // client has to send the initial connect packet, default 200ms ConnectTimeout time.Duration // set to true for additional log information Debug bool ShowSettings bool // contains filtered or unexported fields }
func (*Server) Signal ¶ added in v0.10.0
func (s *Server) Signal() <-chan interface{}
Signal returns a channel used by server to inform the application layer of events. E.g event.ServerStop
Directories ¶
Path | Synopsis |
---|---|
Package arn provides an MQTT topic filter matcher.
|
Package arn provides an MQTT topic filter matcher. |
cmd
|
|
tt
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 ttx provides test types
|
Package ttx provides test types |
Click to show internal directories.
Click to hide internal directories.