diff --git a/client.go b/client.go index 4a20f53..34e61ce 100644 --- a/client.go +++ b/client.go @@ -18,8 +18,8 @@ var defaultOpts = options{ //https://faye.jcoglan.com/architecture.html type client interface { - Subscribe(subscription string, onMsg func(message *message.Message)) error - Publish(subscription string, message *message.Message) error + Subscribe(subscription string, onMsg func(data message.Data)) error + Publish(subscription string, data message.Data) error //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) } -func (c *Client) Publish(subscription string, message *message.Message) error { - return c.opts.transport.Publish(subscription, message) +func (c *Client) Publish(subscription string, data message.Data) error { + return c.opts.transport.Publish(subscription, data) } diff --git a/message/message.go b/message/message.go index a57670a..0ab19ab 100644 --- a/message/message.go +++ b/message/message.go @@ -4,6 +4,8 @@ import "errors" type Extension func(message *Message) +type Data = interface{} + type Message struct { Channel string `json:"channel,omitempty"` Version string `json:"version,omitempty"` @@ -15,7 +17,7 @@ type Message struct { Id string `json:"id,omitempty"` ClientId string `json:"clientId,omitempty"` Advice Advise `json:"advice,omitempty"` - Data interface{} `json:"data,omitempty"` + Data Data `json:"data,omitempty"` Timestamp uint64 `json:"timestamp,omitempty"` AuthSuccessful bool `json:"authSuccessful,omitempty"` Error string `json:"error,omitempty"` diff --git a/transport/transport.go b/transport/transport.go index 8ce3bab..5c4cfa9 100644 --- a/transport/transport.go +++ b/transport/transport.go @@ -18,9 +18,9 @@ type Transport interface { Options() *Options Handshake() 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 - Publish(subscription string, message *message.Message) error + Publish(subscription string, message message.Data) error } type Event = string diff --git a/transport/websocket/websocket.go b/transport/websocket/websocket.go index 23b27c1..6b06023 100644 --- a/transport/websocket/websocket.go +++ b/transport/websocket/websocket.go @@ -160,7 +160,7 @@ func (w *Websocket) Connect() error { 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{ Channel: transport.Subscribe, ClientId: w.clientID, @@ -184,7 +184,7 @@ func (w *Websocket) Subscribe(subscription string, onMessage func(message *messa if inMsg.GetError() != nil { return inMsg.GetError() } - onMessage(inMsg) + onMessage(inMsg.Data) } return nil } @@ -200,7 +200,7 @@ func (w *Websocket) Unsubscribe(subscription string) error { 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") }