Compare commits

...

4 Commits

4 changed files with 52 additions and 26 deletions

View File

@ -31,7 +31,7 @@ type client interface {
// Option set the Client options, such as Transport, message extensions,etc. // Option set the Client options, such as Transport, message extensions,etc.
type Option func(*options) type Option func(*options)
var _ client = (*Client)(nil) //var _ client = (*Client)(nil)
// Client represents a client connection to an faye server. // Client represents a client connection to an faye server.
type Client struct { type Client struct {
@ -58,14 +58,14 @@ func NewClient(url string, opts ...Option) (*Client, error) {
} }
// Subscribe informs the server that messages published to that channel are delivered to itself. // Subscribe informs the server that messages published to that channel are delivered to itself.
func (c *Client) Subscribe(channel string) (*subscription.Subscription, error) { func (c *Client) Subscribe(channel string, authToken string) (*subscription.Subscription, error) {
return c.dispatcher.Subscribe(channel) return c.dispatcher.Subscribe(channel, authToken)
} }
// Publish publishes events on a channel by sending event messages, the server MAY respond to a publish event // 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. // if this feature is supported by the server use the OnPublishResponse to get the publish status.
func (c *Client) Publish(channel string, data message.Data) (err error) { func (c *Client) Publish(channel string, authToken string, data message.Data) (err error) {
return c.dispatcher.Publish(channel, data) return c.dispatcher.Publish(channel, authToken, data)
} }
// Disconnect closes all subscriptions and inform the server to remove any client-related state. // Disconnect closes all subscriptions and inform the server to remove any client-related state.

View File

@ -10,6 +10,7 @@ import (
"strconv" "strconv"
"sync" "sync"
"sync/atomic" "sync/atomic"
"time"
) )
type Dispatcher struct { type Dispatcher struct {
@ -112,12 +113,21 @@ func (d *Dispatcher) dispatchMessage(msg *message.Message) {
return return
} }
log.Println("Websocket terminated: reconnecting") log.Println("Websocket terminated: reconnecting")
err := d.Disconnect()
if err != nil {
log.Println(err)
}
d.Connect() d.Connect()
if err != nil {
log.Println(err)
}
subsList := d.store.GetAll() subsList := d.store.GetAll()
for i := range subsList { for i := range subsList {
sub := subsList[i] sub := subsList[i]
d.Subscribe(sub.Name()) d.Subscribe(sub.Name(), sub.AuthToken())
} }
case message.MetaSubscribe: case message.MetaSubscribe:
//handle MetaSubscribe resp //handle MetaSubscribe resp
d.pendingSubsMu.Lock() d.pendingSubsMu.Lock()
@ -187,13 +197,14 @@ func (d *Dispatcher) sendMessage(m *message.Message) error {
return d.transport.SendMessage(m) return d.transport.SendMessage(m)
} }
func (d *Dispatcher) Subscribe(channel string) (*subscription.Subscription, error) { func (d *Dispatcher) Subscribe(channel string, authToken string) (*subscription.Subscription, error) {
id := d.nextMsgID() id := d.nextMsgID()
m := &message.Message{ m := &message.Message{
Channel: message.MetaSubscribe, Channel: message.MetaSubscribe,
ClientId: d.clientID, ClientId: d.clientID,
Subscription: channel, Subscription: channel,
Id: id, Id: id,
Ext: map[string]string{"access_token": authToken, "timestamp": string(time.Now().Unix())},
} }
d.extensions.ApplyOutExtensions(m) d.extensions.ApplyOutExtensions(m)
@ -208,7 +219,7 @@ func (d *Dispatcher) Subscribe(channel string) (*subscription.Subscription, erro
d.pendingSubs[id] = subscriptionConfirmation d.pendingSubs[id] = subscriptionConfirmation
d.pendingSubsMu.Unlock() d.pendingSubsMu.Unlock()
sub, err := subscription.NewSubscription(channel, d.Unsubscribe, inMsgCh) sub, err := subscription.NewSubscription(channel, d.Unsubscribe, authToken, inMsgCh)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -245,7 +256,7 @@ func (d *Dispatcher) Unsubscribe(sub *subscription.Subscription) error {
return nil return nil
} }
func (d *Dispatcher) Publish(subscription string, data message.Data) (err error) { func (d *Dispatcher) Publish(subscription string, authToken string, data message.Data) (err error) {
id := d.nextMsgID() id := d.nextMsgID()
m := &message.Message{ m := &message.Message{
@ -253,6 +264,7 @@ func (d *Dispatcher) Publish(subscription string, data message.Data) (err error)
Data: data, Data: data,
ClientId: d.clientID, ClientId: d.clientID,
Id: id, Id: id,
Ext: map[string]string{"access_token": authToken, "timestamp": string(time.Now().Unix())},
} }
//ack from server //ack from server

View File

@ -12,17 +12,19 @@ type Unsubscriber func(subscription *Subscription) error
type Subscription struct { type Subscription struct {
channel string channel string
authToken string
unsub Unsubscriber unsub Unsubscriber
msgCh chan *message.Message msgCh chan *message.Message
} }
// todo error // todo error
func NewSubscription(chanel string, unsub Unsubscriber, msgCh chan *message.Message) (*Subscription, error) { func NewSubscription(chanel string, unsub Unsubscriber, authToken string, msgCh chan *message.Message) (*Subscription, error) {
if !IsValidSubscriptionName(chanel) { if !IsValidSubscriptionName(chanel) {
return nil, ErrInvalidChannelName return nil, ErrInvalidChannelName
} }
return &Subscription{ return &Subscription{
channel: chanel, channel: chanel,
authToken: authToken,
unsub: unsub, unsub: unsub,
msgCh: msgCh, msgCh: msgCh,
}, nil }, nil
@ -30,16 +32,19 @@ func NewSubscription(chanel string, unsub Unsubscriber, msgCh chan *message.Mess
func (s *Subscription) OnMessage(onMessage func(channel string, msg message.Data)) error { func (s *Subscription) OnMessage(onMessage func(channel string, msg message.Data)) error {
var inMsg *message.Message var inMsg *message.Message
go func() error { go s.StartMessageLoop(inMsg, onMessage)
return nil
}
func (s *Subscription) StartMessageLoop(inMsg *message.Message, callback func(channel string, msg message.Data)) error {
for inMsg = range s.msgCh { for inMsg = range s.msgCh {
if inMsg.GetError() != nil { if inMsg.GetError() != nil {
return inMsg.GetError() return inMsg.GetError()
} }
onMessage(inMsg.Channel, inMsg.Data) callback(inMsg.Channel, inMsg.Data)
} }
return nil
}()
return nil return nil
} }
@ -56,6 +61,10 @@ func (s *Subscription) Unsubscribe() error {
return s.unsub(s) return s.unsub(s)
} }
func (s *Subscription) AuthToken() string {
return s.authToken
}
// validChannelName channel specifies is the channel is in the format /foo/432/bar // validChannelName channel specifies is the channel is in the format /foo/432/bar
var validChannelName = regexp.MustCompile(`^\/(((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@)))+(\/(((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@)))+)*$`) var validChannelName = regexp.MustCompile(`^\/(((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@)))+(\/(((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@)))+)*$`)

View File

@ -46,6 +46,10 @@ func (w *Websocket) Init(endpoint string, options *transport.Options) error {
w.stopCh = make(chan error) w.stopCh = make(chan error)
w.conn, _, err = websocket.DefaultDialer.Dial(endpoint, options.Headers) w.conn, _, err = websocket.DefaultDialer.Dial(endpoint, options.Headers)
if err != nil {
return err
}
err = w.conn.UnderlyingConn().(*tls.Conn).NetConn().(*net.TCPConn).SetKeepAlive(true) err = w.conn.UnderlyingConn().(*tls.Conn).NetConn().(*net.TCPConn).SetKeepAlive(true)
if err != nil { if err != nil {
return err return err
@ -138,7 +142,8 @@ func (w *Websocket) Handshake(msg *message.Message) (resp *message.Message, err
// a connection is established by sending a message to the /meta/connect channel // a connection is established by sending a message to the /meta/connect channel
func (w *Websocket) Connect(msg *message.Message) error { func (w *Websocket) Connect(msg *message.Message) error {
go func() { go func() {
log.Fatal(w.readWorker()) err := w.readWorker()
log.Fatal(err)
}() }()
return w.SendMessage(msg) return w.SendMessage(msg)
} }