Documentation ¶
Overview ¶
Package transport implements functionality for handling MQTT connections.
Example ¶
// launch server server, err := Launch("tcp://localhost:1337") if err != nil { panic(err) } go func() { // accept next incoming connection conn, err := server.Accept() if err != nil { panic(err) } // receive next packet pkt, err := conn.Receive() if err != nil { panic(err) } // check packet type if _, ok := pkt.(*packet.Connect); ok { // send a connack packet err = conn.Send(packet.NewConnack()) if err != nil { panic(err) } } else { panic("unexpected packet") } }() // dial to server conn, err := Dial("tcp://localhost:1337") if err != nil { panic(err) } // send connect packet err = conn.Send(packet.NewConnect()) if err != nil { panic(err) } // receive next packet pkt, err := conn.Receive() if err != nil { panic(err) } // check packet type if connackPacket, ok := pkt.(*packet.Connack); ok { fmt.Println(connackPacket) // close connection err = conn.Close() if err != nil { panic(err) } } else { panic("unexpected packet") } // close server err = server.Close() if err != nil { panic(err) }
Output: <Connack SessionPresent=false ReturnCode=0>
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrAcceptAfterClose = errors.New("accept after close")
ErrAcceptAfterClose can be returned by a WebSocketServer during Accept() if the server has been already closed and the internal goroutine is dying.
Note: this error is wrapped in an Error with NetworkError code.
var ErrNotBinary = errors.New("received web socket message is not binary")
ErrNotBinary may be returned by WebSocket connection when a message is received that is not binary.
var ErrUnsupportedProtocol = errors.New("unsupported protocol")
ErrUnsupportedProtocol is returned if either the launcher or dialer couldn't infer the protocol from the URL.
Functions ¶
This section is empty.
Types ¶
type BaseConn ¶
type BaseConn struct {
// contains filtered or unexported fields
}
A BaseConn manages the low-level plumbing between the Carrier and the packet Stream.
func NewBaseConn ¶
NewBaseConn creates a new BaseConn using the specified Carrier.
func (*BaseConn) BufferedSend ¶
BufferedSend will write the packet to an internal buffer. It will flush the internal buffer automatically when it gets stale. Encoding errors are directly returned as in Send, but any network errors caught while flushing the buffer at a later time will be returned on the next call.
Note: Only one goroutine can call BufferedSend at the same time.
func (*BaseConn) Close ¶
Close will close the underlying connection and cleanup resources. It will return an Error if there was an error while closing the underlying connection.
func (*BaseConn) Receive ¶
Receive will read from the underlying connection and return a fully read packet. It will return an Error if there was an error while decoding or reading from the underlying connection.
Note: Only one goroutine can Receive at the same time.
func (*BaseConn) Send ¶
Send will write the packet to the underlying connection. It will return an Error if there was an error while encoding or writing to the underlying connection.
Note: Only one goroutine can Send at the same time.
func (*BaseConn) SetReadLimit ¶
SetReadLimit sets the maximum size of a packet that can be received. If the limit is greater than zero, Receive will close the connection and return an Error if receiving the next packet will exceed the limit.
func (*BaseConn) SetReadTimeout ¶
SetReadTimeout sets the maximum time that can pass between reads. If no data is received in the set duration the connection will be closed and Read returns an error.
type Carrier ¶
type Carrier interface { io.ReadWriteCloser SetReadDeadline(time.Time) error }
A Carrier is a generalized stream that can be used with BaseConn.
type Conn ¶
type Conn interface { // Send will write the packet to the underlying connection. It will return // an Error if there was an error while encoding or writing to the // underlying connection. // // Note: Only one goroutine can Send at the same time. Send(pkt packet.Generic) error // BufferedSend will write the packet to an internal buffer. It will flush // the internal buffer automatically when it gets stale. Encoding errors are // directly returned as in Send, but any network errors caught while flushing // the buffer at a later time will be returned on the next call. // // Note: Only one goroutine can call BufferedSend at the same time. BufferedSend(pkt packet.Generic) error // Receive will read from the underlying connection and return a fully read // packet. It will return an Error if there was an error while decoding or // reading from the underlying connection. // // Note: Only one goroutine can Receive at the same time. Receive() (packet.Generic, error) // Close will close the underlying connection and cleanup resources. It will // return an Error if there was an error while closing the underlying // connection. Close() error // SetReadLimit sets the maximum size of a packet that can be received. // If the limit is greater than zero, Receive will close the connection and // return an Error if receiving the next packet will exceed the limit. SetReadLimit(limit int64) // SetReadTimeout sets the maximum time that can pass between reads. // If no data is received in the set duration the connection will be closed // and Read returns an error. SetReadTimeout(timeout time.Duration) // SetBuffers will set the size of the operating system buffers. SetBuffers(read, write int) // LocalAddr will return the underlying connection's local net address. LocalAddr() net.Addr // RemoteAddr will return the underlying connection's remote net address. RemoteAddr() net.Addr }
A Conn is a connection between a client and a broker. It abstracts an existing underlying stream connection.
type Dialer ¶
type Dialer struct { TLSConfig *tls.Config RequestHeader http.Header DefaultTCPPort string DefaultTLSPort string DefaultWSPort string DefaultWSSPort string // contains filtered or unexported fields }
The Dialer handles connecting to a server and creating a connection.
type NetConn ¶
type NetConn struct { BaseConn // contains filtered or unexported fields }
A NetConn is a wrapper around a basic TCP connection.
func (*NetConn) RemoteAddr ¶
RemoteAddr returns the remote network address.
func (*NetConn) SetBuffers ¶ added in v0.6.0
SetBuffers allows to set the buffer sizes used by the operating system.
func (*NetConn) UnderlyingConn ¶
UnderlyingConn returns the underlying net.Conn.
type NetServer ¶
type NetServer struct {
// contains filtered or unexported fields
}
A NetServer accepts net.Conn based connections.
func NewNetServer ¶
NewNetServer creates a new TCP server that listens on the provided address.
func NewSecureNetServer ¶
NewSecureNetServer creates a new TLS server that listens on the provided address.
func (*NetServer) Accept ¶
Accept will return the next available connection or block until a connection becomes available, otherwise returns an Error.
type Server ¶
type Server interface { // Accept will return the next available connection or block until a // connection becomes available, otherwise returns an Error. Accept() (Conn, error) // Close will close the underlying listener and cleanup resources. It will // return an Error if the underlying listener didn't close cleanly. Close() error // Addr returns the server's network address. Addr() net.Addr }
A Server is a local port on which incoming connections can be accepted.
type WebSocketConn ¶
type WebSocketConn struct { BaseConn // contains filtered or unexported fields }
The WebSocketConn wraps a websocket.Conn. The implementation supports packets that are chunked over several WebSocket messages and packets that are coalesced to one WebSocket message.
func NewWebSocketConn ¶
func NewWebSocketConn(conn *websocket.Conn) *WebSocketConn
NewWebSocketConn returns a new WebSocketConn.
func (*WebSocketConn) LocalAddr ¶
func (c *WebSocketConn) LocalAddr() net.Addr
LocalAddr returns the local network address.
func (*WebSocketConn) RemoteAddr ¶
func (c *WebSocketConn) RemoteAddr() net.Addr
RemoteAddr returns the remote network address.
func (*WebSocketConn) SetBuffers ¶ added in v0.6.0
func (c *WebSocketConn) SetBuffers(read, write int)
SetBuffers allows to set the buffer sizes used by the operating system.
func (*WebSocketConn) UnderlyingConn ¶
func (c *WebSocketConn) UnderlyingConn() *websocket.Conn
UnderlyingConn returns the underlying websocket.Conn.
type WebSocketServer ¶
type WebSocketServer struct {
// contains filtered or unexported fields
}
The WebSocketServer accepts websocket.Conn based connections.
func NewSecureWebSocketServer ¶
func NewSecureWebSocketServer(address string, config *tls.Config) (*WebSocketServer, error)
NewSecureWebSocketServer creates a new WSS server that listens on the provided address.
func NewWebSocketServer ¶
func NewWebSocketServer(address string) (*WebSocketServer, error)
NewWebSocketServer creates a new WS server that listens on the provided address.
func (*WebSocketServer) Accept ¶
func (s *WebSocketServer) Accept() (Conn, error)
Accept will return the next available connection or block until a connection becomes available, otherwise returns an Error.
func (*WebSocketServer) Addr ¶
func (s *WebSocketServer) Addr() net.Addr
Addr returns the server's network address.
func (*WebSocketServer) Close ¶
func (s *WebSocketServer) Close() error
Close will close the underlying listener and cleanup resources. It will return an Error if the underlying listener didn't close cleanly.
func (*WebSocketServer) SetFallback ¶
func (s *WebSocketServer) SetFallback(handler http.Handler)
SetFallback will register a http.Handler that gets called if a request is not a WebSocket upgrade request.
func (*WebSocketServer) SetOriginChecker ¶
func (s *WebSocketServer) SetOriginChecker(fn func(r *http.Request) bool)
SetOriginChecker sets an optional function that allows check the request origin before accepting the connection.