fayec/client.go

108 lines
3.5 KiB
Go
Raw Permalink Normal View History

2018-09-05 09:05:16 +00:00
package fayec
2018-09-04 22:55:52 +00:00
import (
2023-09-19 02:01:38 +00:00
"gitea.watsonlabs.net/watsonb8/fayec/internal/dispatcher"
"gitea.watsonlabs.net/watsonb8/fayec/message"
"gitea.watsonlabs.net/watsonb8/fayec/subscription"
"gitea.watsonlabs.net/watsonb8/fayec/transport"
_ "gitea.watsonlabs.net/watsonb8/fayec/transport/websocket"
2018-09-04 22:55:52 +00:00
)
type options struct {
transport transport.Transport
transportOpts transport.Options
extensions message.Extensions
2018-09-04 22:55:52 +00:00
}
var defaultOpts = options{
transport: transport.GetTransport("websocket"),
}
2023-09-19 01:58:15 +00:00
// https://faye.jcoglan.com/architecture.html
2018-09-04 22:55:52 +00:00
type client interface {
Disconnect() error
Subscribe(subscription string) (*subscription.Subscription, error)
Publish(subscription string, message message.Data) error
//SetOnTransportDownHandler(onTransportDown func(err error))
//SetOnTransportUpHandler(onTransportUp func())
2018-09-04 22:55:52 +00:00
}
2023-09-19 01:58:15 +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-04 22:55:52 +00:00
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
dispatcher *dispatcher.Dispatcher
2018-09-04 22:55:52 +00:00
}
2023-09-19 01:58:15 +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)
}
c.dispatcher = dispatcher.NewDispatcher(url, c.opts.transportOpts, c.opts.extensions)
c.dispatcher.SetTransport(c.opts.transport)
err := c.dispatcher.Connect()
if err != nil {
2018-09-05 09:04:10 +00:00
return nil, err
}
2018-09-04 22:55:52 +00:00
return &c, nil
}
2023-09-19 01:58:15 +00:00
// Subscribe informs the server that messages published to that channel are delivered to itself.
func (c *Client) Subscribe(channel string, authToken string) (*subscription.Subscription, error) {
return c.dispatcher.Subscribe(channel, authToken)
}
2023-09-19 01:58:15 +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.
func (c *Client) Publish(channel string, authToken string, data message.Data) (err error) {
return c.dispatcher.Publish(channel, authToken, data)
}
2023-09-19 01:58:15 +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.dispatcher.Disconnect()
}
2023-09-19 01:58:15 +00:00
// WithOutExtension append the provided outgoing extension to the the default transport options
// 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.extensions.Out = append(o.extensions.Out, extension)
}
}
2023-09-19 01:58:15 +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.extensions.In = append(o.extensions.In, inExt)
o.extensions.Out = append(o.extensions.Out, outExt)
2018-09-05 09:01:36 +00:00
}
}
2023-09-19 01:58:15 +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.extensions.In = append(o.extensions.In, extension)
2018-09-05 09:01:36 +00:00
}
}
2018-09-05 09:04:10 +00:00
2023-09-19 01:58:15 +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
}
}