示例#1
0
文件: types.go 项目: kvangork/victor
func (r Room) CacheKey() string {
	return adapter.RoomKey(r.Id())
}
示例#2
0
func (a *Adapter) Listen(outgoing chan adapter.Message) error {
	rooms, err := a.bootstrap()
	if err != nil {
		return err
	}

	streams := []*campfire.Stream{}
	incoming := make(chan *campfire.Message)

	for _, room := range rooms {
		s := room.Stream()
		go s.Connect()
		go func() {
			for {
				message, ok := <-s.Messages()
				if !ok {
					break
				}

				incoming <- message
			}
		}()
		streams = append(streams, s)
	}

	for {
		select {
		case <-a.stop:
			log.Println("Disconnecting from streams")
			for _, s := range streams {
				s.Disconnect()
			}

			log.Println("Leaving rooms")
			for _, r := range rooms {
				r.Leave()
			}

			close(incoming)
			return nil
		case m := <-incoming:
			roomId := itoa(m.RoomId)
			userId := itoa(m.UserId)

			key := adapter.UserKey(userId)

			if !a.brain.Exists(key) || a.brain.Expired(key) {
				user, err := a.client.UserForId(m.UserId)

				if err != nil {
					break
				}

				a.brain.Store(User{user})
			}

			outgoing <- &Message{
				message: m,
				room:    a.brain.Get(adapter.RoomKey(roomId)).(Room),
				user:    a.brain.Get(adapter.UserKey(userId)).(User),
			}
		}
	}
}