Skip to content

Commit

Permalink
add subscription/channel; notes about CORS
Browse files Browse the repository at this point in the history
  • Loading branch information
thisisaaronland committed May 26, 2021
1 parent 852cd46 commit cc6bcda
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
2 changes: 2 additions & 0 deletions broker/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ func (b *Broker) HandlerFunc() (http.HandlerFunc, error) {
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")

// For CORS stuff please use https://github.com/rs/cors

// Don't close the connection, instead loop 10 times,
// sending messages and flushing the response each time
// there is a new message to send along.
Expand Down
37 changes: 37 additions & 0 deletions subscription/channel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package subscription

import (
"context"
)

type ChannelSubscription struct {
Subscription
channel chan string
}

func NewChannelSubscriptionWithChannel(ctx context.Context, ch chan string) (Subscription, error) {

sub := &ChannelSubscription{
channel: ch,
}

return sub, nil
}

func (sub *ChannelSubscription) Start(ctx context.Context, messages_ch chan string) error {

for {
select {
case <-ctx.Done():
return nil
case msg := <-sub.channel:
messages_ch <- msg
}
}

return nil
}

func (sub *ChannelSubscription) Close() error {
return nil
}

0 comments on commit cc6bcda

Please sign in to comment.