2016-07-17 11:59:07 +08:00
|
|
|
package nats
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/nats-io/nats"
|
|
|
|
)
|
|
|
|
|
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-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-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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-10-01 23:23:40 +08:00
|
|
|
func (a *Adaptor) Connect() (errs []error) {
|
2016-07-17 11:59:07 +08:00
|
|
|
|
|
|
|
auth := ""
|
|
|
|
if a.username != "" && a.password != "" {
|
|
|
|
auth = a.username + ":" + a.password + "@"
|
|
|
|
}
|
|
|
|
|
|
|
|
defaultURL := "nats://" + auth + a.Host
|
|
|
|
|
|
|
|
var err error
|
|
|
|
a.client, err = nats.Connect(defaultURL)
|
|
|
|
if err != nil {
|
|
|
|
return append(errs, err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disconnect from the nats server. Returns an error if the client doesn't exist.
|
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-10-01 23:23:40 +08:00
|
|
|
func (a *Adaptor) Finalize() (errs []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
|
|
|
|
}
|