Use interfaces and callbacks
This commit is contained in:
parent
784cfe93c1
commit
eeb2f88b97
46
real_time.go
46
real_time.go
@ -47,13 +47,28 @@ type LikeEvent struct {
|
|||||||
Message Message
|
Message Message
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type EventType = int
|
||||||
|
|
||||||
|
const (
|
||||||
|
EventMessage EventType = iota
|
||||||
|
EventLike
|
||||||
|
)
|
||||||
|
|
||||||
|
type Handler interface {
|
||||||
|
HandleError(error)
|
||||||
|
}
|
||||||
|
type HandlerText interface {
|
||||||
|
HandleTextMessage(Message)
|
||||||
|
}
|
||||||
|
type HandlerLike interface {
|
||||||
|
HandleLike(Message)
|
||||||
|
}
|
||||||
|
|
||||||
//PushSubscription manages real time subscription
|
//PushSubscription manages real time subscription
|
||||||
type PushSubscription struct {
|
type PushSubscription struct {
|
||||||
channel chan wray.Message
|
channel chan wray.Message
|
||||||
//Channel to return things
|
|
||||||
MessageChannel chan Message
|
|
||||||
LikeChannel chan LikeEvent
|
|
||||||
fayeClient *wray.FayeClient
|
fayeClient *wray.FayeClient
|
||||||
|
handlers []Handler
|
||||||
}
|
}
|
||||||
|
|
||||||
//NewPushSubscription creates and returns a push subscription object
|
//NewPushSubscription creates and returns a push subscription object
|
||||||
@ -61,15 +76,17 @@ func NewPushSubscription(context context.Context) PushSubscription {
|
|||||||
|
|
||||||
r := PushSubscription{
|
r := PushSubscription{
|
||||||
channel: make(chan wray.Message),
|
channel: make(chan wray.Message),
|
||||||
MessageChannel: make(chan Message),
|
|
||||||
LikeChannel: make(chan LikeEvent),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *PushSubscription) AddHandler(h Handler) {
|
||||||
|
r.handlers = append(r.handlers, h)
|
||||||
|
}
|
||||||
|
|
||||||
//Listen connects to GroupMe. Runs in Goroutine.
|
//Listen connects to GroupMe. Runs in Goroutine.
|
||||||
func (r *PushSubscription) Listen(context context.Context) {
|
func (r *PushSubscription) StartListening(context context.Context) {
|
||||||
r.fayeClient = wray.NewFayeClient(pushServer)
|
r.fayeClient = wray.NewFayeClient(pushServer)
|
||||||
|
|
||||||
r.fayeClient.SetLogger(fayeLogger{})
|
r.fayeClient.SetLogger(fayeLogger{})
|
||||||
@ -79,6 +96,7 @@ func (r *PushSubscription) Listen(context context.Context) {
|
|||||||
|
|
||||||
go r.fayeClient.Listen()
|
go r.fayeClient.Listen()
|
||||||
|
|
||||||
|
go func() {
|
||||||
for {
|
for {
|
||||||
msg := <-r.channel
|
msg := <-r.channel
|
||||||
data := msg.Data()
|
data := msg.Data()
|
||||||
@ -92,8 +110,12 @@ func (r *PushSubscription) Listen(context context.Context) {
|
|||||||
out := Message{}
|
out := Message{}
|
||||||
json.Unmarshal(b, &out)
|
json.Unmarshal(b, &out)
|
||||||
//fmt.Printf("%+v\n", out) //TODO
|
//fmt.Printf("%+v\n", out) //TODO
|
||||||
|
for _, h := range r.handlers {
|
||||||
|
if h, ok := h.(HandlerText); ok {
|
||||||
|
h.HandleTextMessage(out)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
r.MessageChannel <- out
|
|
||||||
break
|
break
|
||||||
case "like.create":
|
case "like.create":
|
||||||
b, _ := json.Marshal(content.(map[string]interface{})["line"])
|
b, _ := json.Marshal(content.(map[string]interface{})["line"])
|
||||||
@ -104,10 +126,11 @@ func (r *PushSubscription) Listen(context context.Context) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
outt := LikeEvent{Message: out}
|
for _, h := range r.handlers {
|
||||||
//fmt.Printf("Like on %+v \n", outt.Message)
|
if h, ok := h.(HandlerLike); ok {
|
||||||
|
h.HandleLike(out)
|
||||||
r.LikeChannel <- outt
|
}
|
||||||
|
}
|
||||||
break
|
break
|
||||||
case "ping":
|
case "ping":
|
||||||
break
|
break
|
||||||
@ -121,6 +144,7 @@ func (r *PushSubscription) Listen(context context.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
//SubscribeToUser to users
|
//SubscribeToUser to users
|
||||||
|
Loading…
Reference in New Issue
Block a user