allow subscription to publish to itself
This commit is contained in:
parent
d8a246f014
commit
58a5ed8a3f
@ -4,19 +4,22 @@ import (
|
|||||||
"github.com/thesyncim/faye/message"
|
"github.com/thesyncim/faye/message"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Unsubscriber interface {
|
type Unsubscriber func(subscription *Subscription) error
|
||||||
Unsubscribe(subscription *Subscription) error
|
|
||||||
}
|
type Publisher func(msg message.Data) (string, error)
|
||||||
|
|
||||||
type Subscription struct {
|
type Subscription struct {
|
||||||
id string //request Subscription ID
|
id string //request Subscription ID
|
||||||
channel string
|
channel string
|
||||||
ok chan error //used by
|
ok chan error //used by
|
||||||
unsub Unsubscriber
|
unsub Unsubscriber
|
||||||
|
pub Publisher
|
||||||
msgCh chan *message.Message
|
msgCh chan *message.Message
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSubscription(id string, chanel string, unsub Unsubscriber, msgCh chan *message.Message, ok chan error) *Subscription {
|
func NewSubscription(id string, chanel string, unsub Unsubscriber, pub Publisher, msgCh chan *message.Message, ok chan error) *Subscription {
|
||||||
return &Subscription{
|
return &Subscription{
|
||||||
|
pub: pub,
|
||||||
ok: ok,
|
ok: ok,
|
||||||
id: id,
|
id: id,
|
||||||
channel: chanel,
|
channel: chanel,
|
||||||
@ -53,5 +56,9 @@ func (s *Subscription) SubscriptionResult() chan error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Subscription) Unsubscribe() error {
|
func (s *Subscription) Unsubscribe() error {
|
||||||
return s.unsub.Unsubscribe(s)
|
return s.unsub(s)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Subscription) Publish(msg message.Data) (string, error) {
|
||||||
|
return s.pub(msg)
|
||||||
}
|
}
|
||||||
|
@ -90,11 +90,10 @@ func (w *Websocket) readWorker() error {
|
|||||||
//handle MetaSubscribe resp
|
//handle MetaSubscribe resp
|
||||||
w.subsMu2.Lock()
|
w.subsMu2.Lock()
|
||||||
subscriptions, ok := w.subs2[msg.Subscription]
|
subscriptions, ok := w.subs2[msg.Subscription]
|
||||||
|
if !ok {
|
||||||
|
panic("BUG: subscription not registered `" + msg.Subscription + "`")
|
||||||
|
}
|
||||||
if !msg.Successful {
|
if !msg.Successful {
|
||||||
|
|
||||||
if !ok {
|
|
||||||
panic("BUG: subscription not registered `" + msg.Subscription + "`")
|
|
||||||
}
|
|
||||||
if msg.GetError() == nil {
|
if msg.GetError() == nil {
|
||||||
//inject the error if the server returns unsuccessful without error
|
//inject the error if the server returns unsuccessful without error
|
||||||
msg.Error = fmt.Sprintf("susbscription `%s` failed", msg.Subscription)
|
msg.Error = fmt.Sprintf("susbscription `%s` failed", msg.Subscription)
|
||||||
@ -106,7 +105,7 @@ func (w *Websocket) readWorker() error {
|
|||||||
select {
|
select {
|
||||||
case subscriptions[i].SubscriptionResult() <- msg.GetError():
|
case subscriptions[i].SubscriptionResult() <- msg.GetError():
|
||||||
close(subscriptions[i].MsgChannel())
|
close(subscriptions[i].MsgChannel())
|
||||||
/*default:
|
default:
|
||||||
log.Println("subscription has no listeners") //todo remove*/
|
log.Println("subscription has no listeners") //todo remove*/
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -126,7 +125,6 @@ func (w *Websocket) readWorker() error {
|
|||||||
default:
|
default:
|
||||||
log.Println("subscription has no listeners") //todo remove*/
|
log.Println("subscription has no listeners") //todo remove*/
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -275,11 +273,13 @@ func (w *Websocket) Subscribe(channel string) (*subscription.Subscription, error
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
//todo validate
|
|
||||||
inMsgCh := make(chan *message.Message, 0)
|
inMsgCh := make(chan *message.Message, 0)
|
||||||
subRes := make(chan error)
|
subRes := make(chan error)
|
||||||
|
|
||||||
sub := subscription.NewSubscription(id, channel, w, inMsgCh, subRes)
|
var pub = func(data message.Data) (string, error) {
|
||||||
|
return w.Publish(channel, data)
|
||||||
|
}
|
||||||
|
sub := subscription.NewSubscription(id, channel, w.Unsubscribe, pub, inMsgCh, subRes)
|
||||||
|
|
||||||
w.subsMu2.Lock()
|
w.subsMu2.Lock()
|
||||||
w.subs2[channel] = append(w.subs2[channel], sub)
|
w.subs2[channel] = append(w.subs2[channel], sub)
|
||||||
|
Loading…
Reference in New Issue
Block a user