fayec/client.go

128 lines
4.1 KiB
Go
Raw Normal View History

2018-09-05 09:05:16 +00:00
package fayec
2018-09-04 22:55:52 +00:00
import (
"github.com/thesyncim/faye/message"
"github.com/thesyncim/faye/transport"
2018-09-05 09:12:19 +00:00
_ "github.com/thesyncim/faye/transport/websocket"
2018-09-04 22:55:52 +00:00
)
type options struct {
inExt []message.Extension
outExt []message.Extension
2018-09-04 23:04:40 +00:00
transport transport.Transport
2018-09-04 22:55:52 +00:00
}
var defaultOpts = options{
transport: transport.GetTransport("websocket"),
}
//https://faye.jcoglan.com/architecture.html
type client interface {
Disconnect() error
Subscribe(subscription string, onMessage func(message message.Data)) error
Unsubscribe(subscription string) error
2018-09-06 13:29:49 +00:00
Publish(subscription string, message message.Data) (string, error)
OnPublishResponse(subscription string, onMsg func(message *message.Message))
2018-09-04 22:55:52 +00:00
}
2018-09-06 14:03:24 +00:00
//Option set the Client options, such as Transport, message extensions,etc.
2018-09-04 22:55:52 +00:00
type Option func(*options)
var _ client = (*Client)(nil)
2018-09-06 14:03:24 +00:00
// Client represents a client connection to an faye server.
2018-09-04 22:55:52 +00:00
type Client struct {
opts options
}
2018-09-06 14:03:24 +00:00
//NewClient creates a new faye client with the provided options and connect to the specified url.
2018-09-04 22:55:52 +00:00
func NewClient(url string, opts ...Option) (*Client, error) {
var c Client
c.opts = defaultOpts
for _, opt := range opts {
opt(&c.opts)
}
2018-09-04 23:04:40 +00:00
tops := &transport.Options{
Url: url,
InExt: c.opts.inExt,
OutExt: c.opts.outExt,
}
2018-09-05 09:04:10 +00:00
var err error
if err = c.opts.transport.Init(tops); err != nil {
return nil, err
}
2018-09-04 23:04:40 +00:00
2018-09-05 09:04:10 +00:00
if err = c.opts.transport.Handshake(); err != nil {
2018-09-04 23:04:40 +00:00
return nil, err
}
2018-09-05 09:04:10 +00:00
if err = c.opts.transport.Connect(); err != nil {
return nil, err
}
2018-09-04 22:55:52 +00:00
return &c, nil
}
2018-09-06 14:03:24 +00:00
//Subscribe informs the server that messages published to that channel are delivered to itself.
func (c *Client) Subscribe(subscription string, onMsg func(message message.Data)) error {
return c.opts.transport.Subscribe(subscription, onMsg)
}
2018-09-06 14:03:24 +00:00
//Unsubscribe informs the server that the client will no longer listen to incoming event messages on
//the specified channel/subscription
func (c *Client) Unsubscribe(subscription string) error {
return c.opts.transport.Unsubscribe(subscription)
}
2018-09-06 14:03:24 +00:00
//Publish publishes events on a channel by sending event messages, the server MAY respond to a publish event
//if this feature is supported by the server use the OnPublishResponse to get the publish status.
2018-09-06 13:29:49 +00:00
func (c *Client) Publish(subscription string, data message.Data) (id string, err error) {
return c.opts.transport.Publish(subscription, data)
}
2018-09-06 13:29:49 +00:00
//OnPublishResponse sets the handler to be triggered if the server replies to the publish request
//according to the spec the server MAY reply to the publish request, so its not guaranteed that this handler will
2018-09-06 14:03:24 +00:00
//ever be triggered.
//can be used to identify the status of the published request and for example retry failed published requests.
2018-09-06 13:29:49 +00:00
func (c *Client) OnPublishResponse(subscription string, onMsg func(message *message.Message)) {
c.opts.transport.OnPublishResponse(subscription, onMsg)
}
2018-09-06 14:03:24 +00:00
//Disconnect closes all subscriptions and inform the server to remove any client-related state.
//any subsequent method call to the client object will result in undefined behaviour.
func (c *Client) Disconnect() error {
return c.opts.transport.Disconnect()
}
2018-09-06 14:03:24 +00:00
//WithOutExtension append the provided outgoing extension to the list of outgoing extensions.
//extensions run in the order that they are provided
2018-09-05 09:01:36 +00:00
func WithOutExtension(extension message.Extension) Option {
return func(o *options) {
o.outExt = append(o.outExt, extension)
}
}
2018-09-06 14:03:24 +00:00
//WithExtension append the provided incoming extension and outgoing to the list of incoming and outgoing extensions.
//extensions run in the order that they are provided
func WithExtension(inExt message.Extension, outExt message.Extension) Option {
return func(o *options) {
o.inExt = append(o.inExt, inExt)
o.outExt = append(o.outExt, outExt)
2018-09-05 09:01:36 +00:00
}
}
2018-09-06 14:03:24 +00:00
//WithInExtension append the provided incoming extension to the list of incoming extensions.
//extensions run in the order that they are provided
2018-09-05 09:01:36 +00:00
func WithInExtension(extension message.Extension) Option {
return func(o *options) {
o.inExt = append(o.inExt, extension)
2018-09-05 09:01:36 +00:00
}
}
2018-09-05 09:04:10 +00:00
2018-09-06 14:03:24 +00:00
//WithTransport sets the client transport to be used to communicate with server.
2018-09-05 09:01:36 +00:00
func WithTransport(t transport.Transport) Option {
return func(o *options) {
o.transport = t
}
}