2016-07-17 11:59:07 +08:00
|
|
|
package nats
|
|
|
|
|
2016-12-02 16:56:37 +08:00
|
|
|
import "github.com/nats-io/nats"
|
2016-07-17 11:59:07 +08:00
|
|
|
|
2016-10-01 23:23:40 +08:00
|
|
|
// Adaptor is a configuration struct for interacting with a NATS server.
|
2016-07-17 11:59:07 +08:00
|
|
|
// Name is a logical name for the adaptor/nats server connection.
|
|
|
|
// Host is in the form "localhost:4222" which is the hostname/ip and port of the nats server.
|
|
|
|
// ClientID is a unique identifier integer that specifies the identity of the client.
|
2016-10-01 23:23:40 +08:00
|
|
|
type Adaptor struct {
|
2016-07-17 11:59:07 +08:00
|
|
|
name string
|
|
|
|
Host string
|
|
|
|
clientID int
|
|
|
|
username string
|
|
|
|
password string
|
|
|
|
client *nats.Conn
|
2016-12-02 16:56:37 +08:00
|
|
|
connect func() (*nats.Conn, error)
|
2016-07-17 11:59:07 +08:00
|
|
|
}
|
|
|
|
|
2016-10-01 23:23:40 +08:00
|
|
|
// NewAdaptor populates a new NATS Adaptor.
|
|
|
|
func NewAdaptor(host string, clientID int) *Adaptor {
|
|
|
|
return &Adaptor{
|
2016-10-04 01:06:37 +08:00
|
|
|
name: "NATS",
|
2016-07-17 11:59:07 +08:00
|
|
|
Host: host,
|
|
|
|
clientID: clientID,
|
2016-12-02 16:56:37 +08:00
|
|
|
connect: func() (*nats.Conn, error) {
|
|
|
|
return nats.Connect("nats://" + host)
|
|
|
|
},
|
2016-07-17 11:59:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-01 23:23:40 +08:00
|
|
|
// NewAdaptorWithAuth populates a NATS Adaptor including username and password.
|
|
|
|
func NewAdaptorWithAuth(host string, clientID int, username string, password string) *Adaptor {
|
|
|
|
return &Adaptor{
|
2016-07-17 11:59:07 +08:00
|
|
|
Host: host,
|
|
|
|
clientID: clientID,
|
|
|
|
username: username,
|
|
|
|
password: password,
|
2016-12-02 16:56:37 +08:00
|
|
|
connect: func() (*nats.Conn, error) {
|
|
|
|
return nats.Connect("nats://" + username + ":" + password + "@" + host)
|
|
|
|
},
|
2016-07-17 11:59:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the logical client name.
|
2016-10-01 23:23:40 +08:00
|
|
|
func (a *Adaptor) Name() string { return a.name }
|
|
|
|
|
|
|
|
// SetName sets the logical client name.
|
|
|
|
func (a *Adaptor) SetName(n string) { a.name = n }
|
2016-07-17 11:59:07 +08:00
|
|
|
|
|
|
|
// Connect makes a connection to the Nats server.
|
2016-11-08 02:55:43 +08:00
|
|
|
func (a *Adaptor) Connect() (err error) {
|
2016-12-02 16:56:37 +08:00
|
|
|
a.client, err = a.connect()
|
2016-07-17 11:59:07 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-12-02 17:05:54 +08:00
|
|
|
// Disconnect from the nats server.
|
2016-10-01 23:23:40 +08:00
|
|
|
func (a *Adaptor) Disconnect() (err error) {
|
2016-07-17 11:59:07 +08:00
|
|
|
if a.client != nil {
|
|
|
|
a.client.Close()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finalize is simply a helper method for the disconnect.
|
2016-11-08 02:55:43 +08:00
|
|
|
func (a *Adaptor) Finalize() (err error) {
|
2016-07-17 11:59:07 +08:00
|
|
|
a.Disconnect()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Publish sends a message with the particular topic to the nats server.
|
2016-10-01 23:23:40 +08:00
|
|
|
func (a *Adaptor) Publish(topic string, message []byte) bool {
|
2016-07-17 11:59:07 +08:00
|
|
|
if a.client == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
a.client.Publish(topic, message)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// On is an event-handler style subscriber to a particular topic (named event).
|
|
|
|
// Supply a handler function to use the bytes returned by the server.
|
2016-10-01 23:23:40 +08:00
|
|
|
func (a *Adaptor) On(event string, f func(s []byte)) bool {
|
2016-07-17 11:59:07 +08:00
|
|
|
if a.client == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
a.client.Subscribe(event, func(msg *nats.Msg) {
|
2016-07-18 06:53:12 +08:00
|
|
|
f(msg.Data)
|
2016-07-17 11:59:07 +08:00
|
|
|
})
|
|
|
|
return true
|
|
|
|
}
|