mqtt

package
v0.0.0-...-9ed3ea8 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2025 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MQTT_REPLY_QOS_HEADER    = "X-MQTT-Reply-QoS"
	MQTT_REPLY_RETAIN_HEADER = "X-MQTT-Reply-Retain"
	MQTT_REPLY_TOPIC_HEADER  = "X-MQTT-Reply-Topic"
)

Variables

This section is empty.

Functions

func ClientFromContext

func ClientFromContext(ctx context.Context) mqtt.Client

func ClientFromServerContext

func ClientFromServerContext(ctx context.Context) (*mqtt.Client, bool)

ClientFromServerContext returns client from context.

func DefaultErrorEncoder

func DefaultErrorEncoder(w http.ResponseWriter, r *http.Request, err error)

DefaultErrorEncoder encodes the error to the HTTP response.

func DefaultRequestDecoder

func DefaultRequestDecoder(r *http.Request, v interface{}) error

func DefaultRequestEncoder

func DefaultRequestEncoder(_ context.Context, contentType string, in interface{}) ([]byte, error)

DefaultRequestEncoder is an MQTT request encoder.

func DefaultRequestVars

func DefaultRequestVars(r *http.Request, v interface{}) error

func DefaultResponseEncoder

func DefaultResponseEncoder(w http.ResponseWriter, r *http.Request, v interface{}) error

func MessageFromContext

func MessageFromContext(ctx context.Context) mqtt.Message

func MessageFromServerContext

func MessageFromServerContext(ctx context.Context) (*mqtt.Message, bool)

MessageFromServerContext returns message from context.

func RequestFromServerContext

func RequestFromServerContext(ctx context.Context) (*http.Request, bool)

RequestFromServerContext returns request from context.

func SetOperation

func SetOperation(ctx context.Context, op string)

SetOperation sets the transport operation.

func WithClient

func WithClient(ctx context.Context, client mqtt.Client) context.Context

func WithMessage

func WithMessage(ctx context.Context, msg mqtt.Message) context.Context

Types

type CallOption

type CallOption interface {
	// contains filtered or unexported methods
}

CallOption configures a Call before it starts or extracts information from a Call after it completes.

func ContentType

func ContentType(contentType string) CallOption

ContentType with request content type.

func Header(header *http.Header) CallOption

Header returns a CallOptions that retrieves the http response header from server reply.

func Operation

func Operation(operation string) CallOption

Operation is serviceMethod call option

func PathTemplate

func PathTemplate(pattern string) CallOption

PathTemplate is http path template

type Client

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

Client is an MQTT client.

func NewClient

func NewClient(ctx context.Context, opts ...ClientOption) (*Client, error)

NewClient returns an MQTT client.

func (*Client) Close

func (client *Client) Close() error

Close tears down the MQTT client.

func (*Client) Publish

func (client *Client) Publish(ctx context.Context, topic string, qos byte, retain bool, args interface{}, opts ...CallOption) error

type ClientOption

type ClientOption func(*clientOptions)

ClientOption is MQTT client option.

func WithMiddleware

func WithMiddleware(m ...middleware.Middleware) ClientOption

WithMiddleware with client middleware.

func WithPublishMQTTFn

func WithPublishMQTTFn(fn PublishMQTTFn) ClientOption

func WithRequestEncoder

func WithRequestEncoder(encoder EncodeRequestFunc) ClientOption

WithRequestEncoder with client request encoder.

func WithTimeout

func WithTimeout(d time.Duration) ClientOption

WithTimeout with client request timeout.

type ContentTypeCallOption

type ContentTypeCallOption struct {
	EmptyCallOption
	ContentType string
}

ContentTypeCallOption is BodyCallOption

type Context

type Context interface {
	context.Context
	Vars() url.Values
	Client() mqtt.Client
	Message() mqtt.Message
	Response() http.ResponseWriter
	Middleware(middleware.Handler) middleware.Handler
	Bind(interface{}) error
	BindVars(interface{}) error
	JSON(interface{}) error
	String(string) error
	Stream(string, io.Reader) error
	Reset(http.ResponseWriter, *http.Request)
}

Context is an MQTT Context.

type EmptyCallOption

type EmptyCallOption struct{}

EmptyCallOption does not alter the Call configuration. It can be embedded in another structure to carry satellite data for use by interceptors.

type EncodeRequestFunc

type EncodeRequestFunc func(ctx context.Context, contentType string, in interface{}) (body []byte, err error)

EncodeRequestFunc is request encode func.

type HandlerFunc

type HandlerFunc func(Context) error

HandlerFunc defines a function to serve MQTT requests.

type HeaderCallOption

type HeaderCallOption struct {
	EmptyCallOption
	// contains filtered or unexported fields
}

HeaderCallOption is retrieve response header for client call

type MQTTResponseWriter

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

func (*MQTTResponseWriter) Header

func (rw *MQTTResponseWriter) Header() http.Header

func (*MQTTResponseWriter) Write

func (rw *MQTTResponseWriter) Write(b []byte) (int, error)

func (*MQTTResponseWriter) WriteHeader

func (rw *MQTTResponseWriter) WriteHeader(statusCode int)

type OperationCallOption

type OperationCallOption struct {
	EmptyCallOption
	Operation string
}

OperationCallOption is set ServiceMethod for client call

type PathTemplateCallOption

type PathTemplateCallOption struct {
	EmptyCallOption
	Pattern string
}

PathTemplateCallOption is set path template for client call

type PublishMQTTFn

type PublishMQTTFn func(topic string, qos byte, retain bool, payload []byte) error

type RouteInfo

type RouteInfo struct {
	Path   string
	Method string
}

RouteInfo is an MQTT route info.

type Router

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

Router is an MQTT router.

func (*Router) Group

func (r *Router) Group(prefix string, filters ...xhttp.FilterFunc) *Router

Group returns a new router group.

func (*Router) Handle

func (r *Router) Handle(method, relativePath string, h HandlerFunc, filters ...xhttp.FilterFunc)

Handle registers a new route with a matcher for the URL path and method.

func (*Router) POST

func (r *Router) POST(path string, h HandlerFunc, m ...xhttp.FilterFunc)

POST registers a new POST route for a path with matching handler in the router.

type Server

type Server struct {
	Handler http.Handler
	// contains filtered or unexported fields
}

Server is an MQTT Topic Route server wrapper.

func NewServer

func NewServer(opts ...ServerOption) *Server

NewServer creates an MQTT server by options.

func (*Server) Endpoint

func (s *Server) Endpoint() (*url.URL, error)

func (*Server) MQTTClient

func (s *Server) MQTTClient() mqtt.Client

func (*Server) MQTTHandler

func (s *Server) MQTTHandler() mqtt.MessageHandler

func (*Server) MakePublishMQTTFn

func (s *Server) MakePublishMQTTFn(timeout time.Duration) PublishMQTTFn

func (*Server) MakeSubscribeMQTTFn

func (s *Server) MakeSubscribeMQTTFn() SubscribeMQTTFn

func (*Server) Route

func (s *Server) Route(prefix string, filters ...xhttp.FilterFunc) *Router

Route registers an MQTT router.

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(res http.ResponseWriter, req *http.Request)

ServeMQTT should write reply headers and data to the ResponseWriter and then return.

func (*Server) Shutdown

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

func (*Server) Start

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

Start start the MQTT server.

func (*Server) Stop

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

Stop stop the MQTT server.

func (*Server) Use

func (s *Server) Use(selector string, m ...middleware.Middleware)

Use uses a service middleware with selector. selector:

  • '/*'
  • '/helloworld.v1.Greeter/*'
  • '/helloworld.v1.Greeter/SayHello'

func (*Server) WalkHandle

func (s *Server) WalkHandle(handle func(method, path string, handler http.HandlerFunc)) error

WalkHandle walks the router and all its sub-routers, calling walkFn for each route in the tree.

func (*Server) WalkRoute

func (s *Server) WalkRoute(fn WalkRouteFunc) error

WalkRoute walks the router and all its sub-routers, calling walkFn for each route in the tree.

type ServerOption

type ServerOption func(*Server)

ServerOption is an MQTT server option.

func ErrorEncoder

func ErrorEncoder(en xhttp.EncodeErrorFunc) ServerOption

ErrorEncoder with error encoder.

func Filter

func Filter(filters ...xhttp.FilterFunc) ServerOption

Filter with MQTT middleware option.

func MethodNotAllowedHandler

func MethodNotAllowedHandler(handler http.Handler) ServerOption

func Middleware

func Middleware(m ...middleware.Middleware) ServerOption

Middleware with service middleware option.

func NotFoundHandler

func NotFoundHandler(handler http.Handler) ServerOption

func RequestDecoder

func RequestDecoder(dec xhttp.DecodeRequestFunc) ServerOption

RequestDecoder with request decoder.

func RequestVarsDecoder

func RequestVarsDecoder(dec xhttp.DecodeRequestFunc) ServerOption

RequestVarsDecoder with request decoder.

func ResponseEncoder

func ResponseEncoder(en xhttp.EncodeResponseFunc) ServerOption

ResponseEncoder with response encoder.

func StrictSlash

func StrictSlash(strictSlash bool) ServerOption

StrictSlash is with mux's StrictSlash If true, when the path pattern is "/path/", accessing "/path" will redirect to the former and vice versa.

func Timeout

func Timeout(timeout time.Duration) ServerOption

Timeout with server timeout.

func WithClientOption

func WithClientOption(option *mqtt.ClientOptions) ServerOption

func WithMustStart

func WithMustStart(mustStart bool) ServerOption

type SubscribeMQTTFn

type SubscribeMQTTFn func(topic string, qos byte) error

type Transport

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

Transport is an MQTT transport.

func (*Transport) Client

func (tr *Transport) Client() *mqtt.Client

Client returns the MQTT client.

func (*Transport) Endpoint

func (tr *Transport) Endpoint() string

Endpoint returns the transport endpoint.

func (*Transport) Kind

func (tr *Transport) Kind() transport.Kind

Kind returns the transport kind.

func (*Transport) Message

func (tr *Transport) Message() *mqtt.Message

Message returns the MQTT message.

func (*Transport) Operation

func (tr *Transport) Operation() string

Operation returns the transport operation.

func (*Transport) ReplyHeader

func (tr *Transport) ReplyHeader() transport.Header

ReplyHeader returns the reply header.

func (*Transport) Request

func (tr *Transport) Request() *http.Request

Request returns the MQTT request.

func (*Transport) RequestHeader

func (tr *Transport) RequestHeader() transport.Header

RequestHeader returns the request header.

type Transporter

type Transporter interface {
	transport.Transporter
	Request() *http.Request
	Client() *mqtt.Client
	Message() *mqtt.Message
}

Transporter is mqtt Transporter

type WalkRouteFunc

type WalkRouteFunc func(RouteInfo) error

WalkRouteFunc is the type of the function called for each route visited by Walk.

Jump to

Keyboard shortcuts

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