func subscribe(emitter *emission.Emitter) { conn, _ := redis.Dial("tcp", ":6379") channel := redis.PubSubConn{conn} channel.Subscribe(CHANNEL_NAME) for { reply := channel.Receive() switch parsed := reply.(type) { case redis.Message: message := string(parsed.Data) emitter.Emit("message", message) } } }
func socketHandler(emitter *emission.Emitter) http.HandlerFunc { return func(response http.ResponseWriter, request *http.Request) { socket, err := upgrader.Upgrade(response, request, nil) fmt.Println("access") if err != nil { log.Println(err) return } channel := make(chan string) handler := func(message string) { channel <- message } emitter.AddListener("message", handler) for message := range channel { err := socket.WriteMessage(websocket.TextMessage, []byte(message)) if err != nil { break } } emitter.RemoveListener("message", handler) socket.Close() } }
// Trigger events on an external event emitter func (u *Peer) NotifyChannel(e *event.E, emitter *emission.Emitter) { emitter.Emit(e.Action, e) }
// Binds Peer to an event, using some external event emitter func (p *Peer) BindChannel(e string, emitter *emission.Emitter) { emitter.On(e, p.receiveNotification) }