2022-08-19 07:07:14 +00:00
|
|
|
package natsio
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/nats-io/nats.go"
|
|
|
|
)
|
|
|
|
|
|
|
|
// JSONEncoder ...
|
|
|
|
type JSONEncoder struct {
|
|
|
|
encConn *nats.EncodedConn
|
|
|
|
}
|
|
|
|
|
|
|
|
// Subscribe ...
|
|
|
|
func (e JSONEncoder) Subscribe(subject string, cb nats.Handler) (*nats.Subscription, error) {
|
|
|
|
sub, err := e.encConn.Subscribe(subject, cb)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("natsio.JSONEncoder.Subscribe err: %v\n", err)
|
2022-08-20 14:21:51 +00:00
|
|
|
} else {
|
|
|
|
log.Printf("natsio.JSONEncoder - subscribed to subject %s successfully\n", subject)
|
2022-08-19 07:07:14 +00:00
|
|
|
}
|
|
|
|
return sub, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Publish ...
|
|
|
|
func (e JSONEncoder) Publish(reply string, data interface{}) error {
|
|
|
|
return e.encConn.Publish(reply, data)
|
|
|
|
}
|