wsocket

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2022 License: MIT Imports: 8 Imported by: 0

README

wsmqtt

MQTT websocket implementation using natiu-mqtt.

Simplicity

It took 255 lines of code to get the first cmd/mqttping up and working. The wsocket package itself was 171 lines at that point.

cmd/mqttping

Program for server discovery and uptime checker. See mqttping.go.

Example output shown below. Network cable unplugged after 6 seconds of runtime.

mqttping -url=ws://dashboard.ci/qa -u=username -pass=123 -keepalive=10s
2022/12/14 15:20:18 connection success
2022/12/14 15:20:19 Ping OK (1.003365536s)
2022/12/14 15:20:20 Ping OK (1.003063079s)
2022/12/14 15:20:21 Ping OK (1.004336657s)
2022/12/14 15:20:22 Ping OK (1.003325537s)
2022/12/14 15:20:23 Ping OK (1.003230131s)
2022/12/14 15:20:33 ping failed:failed to get reader: received continuation frame without text or binary frame

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotConnected = errors.New("client not connected")
)

Functions

This section is empty.

Types

type Client

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

Client shall be safe to use concurrently between two goroutines, one reader (Rx) and one that writes (Tx).

func NewClient

func NewClient(clientID string, config ClientConfig) (*Client, error)

func (*Client) Connect

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

func (*Client) Disconnect

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

Disconnect performs a clean disconnect. If the clean disconnect fails it returns an error. Even if the clean disconnect fails and Disconnect returns error the result of IsConnected will always be false after a call to Disonnect.

func (*Client) IsConnected

func (c *Client) IsConnected() bool

func (*Client) Ping

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

func (*Client) PublishPayload

func (c *Client) PublishPayload(ctx context.Context, topic string, qos mqtt.QoSLevel, payload []byte) error

func (*Client) ReadNextPacket added in v0.1.2

func (c *Client) ReadNextPacket(ctx context.Context) (int, error)

func (*Client) SetOnPublishCallback added in v0.1.2

func (c *Client) SetOnPublishCallback(f func(mqtt.Header, mqtt.VariablesPublish, io.Reader) error)

func (*Client) Subscribe

func (c *Client) Subscribe(ctx context.Context, subReq []mqtt.SubscribeRequest) error

func (*Client) UnsafePrepareRx added in v0.1.2

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

Prepare Tx must be called before sending a message over the RxTx returned by UnsafeRxTx.

func (*Client) UnsafePrepareTx added in v0.1.1

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

Prepare Tx must be called before sending a message over the RxTx returned by UnsafeRxTx.

func (*Client) UnsafeRxTx added in v0.1.1

func (c *Client) UnsafeRxTx() *mqtt.RxTx

UnsafeRxTx returns the underyling RxTx with callback handlers and all. Not safe for concurrent use.

type ClientConfig

type ClientConfig struct {
	// TODO(soypat): Add Will flags/fields?
	URL string
	// MQTT keepalive is amount of seconds between messages before server disconnects client automatically.
	MQTTKeepAlive      uint16
	Username, Password string
	WSOptions          *websocket.DialOptions
	Subs               mqtt.Subscriptions
}

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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