Ejemplo n.º 1
0
func (a *adapter) Send(roomID, msg string) {
	id, _ := strconv.Atoi(roomID)
	room := campfire.Room{
		Connection: a.client.Connection,
		ID:         id,
	}

	err := room.SendText(msg)
	if err != nil {
		log.Printf("Error sending to room %d: %v\n", roomID, err)
	}
}
Ejemplo n.º 2
0
func (a *adapter) Run() {
	run := func(id int) {
		room := campfire.Room{
			Connection: a.client.Connection,
			ID:         id,
		}

		err := room.Join()
		if err != nil {
			log.Printf("Unable to join room %d: %v\n", id, err)
			return
		}

		stream := room.Stream()
		go stream.Connect()
		messages := stream.Messages()

		for {
			select {
			case <-a.stop:
				stream.Disconnect()
				room.Leave()
				return
			case msg := <-messages:
				userID := strconv.Itoa(msg.UserID)
				roomID := strconv.Itoa(msg.RoomID)

				roomName, ok := a.getStoreKey("room.name." + roomID)
				if !ok {
					room, err := a.client.RoomForID(id)
					if err != nil {
						log.Printf("Unable to fetch room %d: %v\n", id, err)
						return
					}

					roomName = room.Name
					a.setStoreKey("room.name."+roomID, room.Name)
				}

				userName, ok := a.getStoreKey("user.name." + userID)
				if !ok {
					user, err := a.client.UserForID(msg.UserID)
					if err != nil {
						log.Printf("Unable to fetch user %d: %v\n", msg.UserID, err)
						continue
					}

					userName = user.Name
					a.setStoreKey("user.name."+userID, user.Name)
				}

				a.robot.Receive(&message{
					userID:   userID,
					userName: userName,
					roomID:   roomID,
					roomName: roomName,
					text:     msg.Body,
				})
			}
		}
	}

	for _, id := range a.roomIDs {
		go run(id)
	}
}