do not expose low level message.Message to publish

This commit is contained in:
Marcelo Pires 2018-09-06 13:03:21 +02:00
parent c8bfbbd8fd
commit eed039f351
4 changed files with 13 additions and 11 deletions

View File

@ -18,8 +18,8 @@ var defaultOpts = options{
//https://faye.jcoglan.com/architecture.html //https://faye.jcoglan.com/architecture.html
type client interface { type client interface {
Subscribe(subscription string, onMsg func(message *message.Message)) error Subscribe(subscription string, onMsg func(data message.Data)) error
Publish(subscription string, message *message.Message) error Publish(subscription string, data message.Data) error
//todo unsubscribe,etc //todo unsubscribe,etc
} }
@ -82,10 +82,10 @@ func WithTransport(t transport.Transport) Option {
} }
} }
func (c *Client) Subscribe(subscription string, onMsg func(message *message.Message)) error { func (c *Client) Subscribe(subscription string, onMsg func(message message.Data)) error {
return c.opts.transport.Subscribe(subscription, onMsg) return c.opts.transport.Subscribe(subscription, onMsg)
} }
func (c *Client) Publish(subscription string, message *message.Message) error { func (c *Client) Publish(subscription string, data message.Data) error {
return c.opts.transport.Publish(subscription, message) return c.opts.transport.Publish(subscription, data)
} }

View File

@ -4,6 +4,8 @@ import "errors"
type Extension func(message *Message) type Extension func(message *Message)
type Data = interface{}
type Message struct { type Message struct {
Channel string `json:"channel,omitempty"` Channel string `json:"channel,omitempty"`
Version string `json:"version,omitempty"` Version string `json:"version,omitempty"`
@ -15,7 +17,7 @@ type Message struct {
Id string `json:"id,omitempty"` Id string `json:"id,omitempty"`
ClientId string `json:"clientId,omitempty"` ClientId string `json:"clientId,omitempty"`
Advice Advise `json:"advice,omitempty"` Advice Advise `json:"advice,omitempty"`
Data interface{} `json:"data,omitempty"` Data Data `json:"data,omitempty"`
Timestamp uint64 `json:"timestamp,omitempty"` Timestamp uint64 `json:"timestamp,omitempty"`
AuthSuccessful bool `json:"authSuccessful,omitempty"` AuthSuccessful bool `json:"authSuccessful,omitempty"`
Error string `json:"error,omitempty"` Error string `json:"error,omitempty"`

View File

@ -18,9 +18,9 @@ type Transport interface {
Options() *Options Options() *Options
Handshake() error Handshake() error
Connect() error Connect() error
Subscribe(subscription string, onMessage func(message *message.Message)) error Subscribe(subscription string, onMessage func(message message.Data)) error
Unsubscribe(subscription string) error Unsubscribe(subscription string) error
Publish(subscription string, message *message.Message) error Publish(subscription string, message message.Data) error
} }
type Event = string type Event = string

View File

@ -160,7 +160,7 @@ func (w *Websocket) Connect() error {
return w.sendMessage(&m) return w.sendMessage(&m)
} }
func (w *Websocket) Subscribe(subscription string, onMessage func(message *message.Message)) error { func (w *Websocket) Subscribe(subscription string, onMessage func(data message.Data)) error {
m := &message.Message{ m := &message.Message{
Channel: transport.Subscribe, Channel: transport.Subscribe,
ClientId: w.clientID, ClientId: w.clientID,
@ -184,7 +184,7 @@ func (w *Websocket) Subscribe(subscription string, onMessage func(message *messa
if inMsg.GetError() != nil { if inMsg.GetError() != nil {
return inMsg.GetError() return inMsg.GetError()
} }
onMessage(inMsg) onMessage(inMsg.Data)
} }
return nil return nil
} }
@ -200,7 +200,7 @@ func (w *Websocket) Unsubscribe(subscription string) error {
return w.sendMessage(m) return w.sendMessage(m)
} }
func (w *Websocket) Publish(subscription string, message *message.Message) error { func (w *Websocket) Publish(subscription string, data message.Data) error {
panic("not implemented") panic("not implemented")
} }