示例#1
0
func (self *ChatroomAction) View(msg *message.Message) {
	if !utils.IsExistInMap(msg.Params, "ctName") {
		response := daemon.NewSimpleMessage(msg.Src,
			"Please input ctName")
		hazel.SendMessage(response)
		return
	}
	conn := db.RedisPool.Get()
	defer conn.Close()

	ctName := msg.Params["ctName"]

	chatrooms, err := redis.Strings(conn.Do("LRANGE", "chatrooms", "0", "-1"))
	if err != nil {
		fmt.Println("redis error:", err)
	}

	var response *message.Message
	if utils.StringInSlice(ctName, chatrooms) != -1 {
		if number, err := redis.Int(conn.Do("LLEN", "chatroom:"+ctName)); err != nil {
			fmt.Println("redis err:", err)
		} else {
			response = daemon.NewSimpleMessage(msg.Src,
				fmt.Sprintf("%d", number))
			// fmt.Sprintf("%d", len(self.ChatroomList[ctName].cid)))
		}
	} else {
		response = daemon.NewSimpleMessage(msg.Src,
			"the chatroom is not existed")
	}
	hazel.SendMessage(response)
}
示例#2
0
func (self *ChatroomAction) Join(msg *message.Message) {
	if !utils.IsExistInMap(msg.Params, "ctName") {
		hazel.SendMessage(daemon.NewSimpleMessage(msg.Src,
			"Please input ctName"))
		return
	}
	ctName := msg.Params["ctName"]

	conn := db.RedisPool.Get()
	defer conn.Close()

	chatrooms, err := redis.Strings(conn.Do("LRANGE", "chatrooms", "0", "-1"))
	if err != nil {
		fmt.Println("redis error:", err)
	}

	// original CHATROOMS -> chatroom
	// now use redis
	if utils.StringInSlice(ctName, chatrooms) != -1 {
		self.Exit(msg)

		// original event manager,
		// now use interface + event trigger, see event.go
		// cid.SetOnCloseListener(self)

		// original way
		// self.UserChatList[msg.Src] = self.ChatroomList[ctName]
		// self.ChatroomList[ctName].cids = append(self.ChatroomList[ctName].cids, msg.Src)

		// now use redis
		err1 := conn.Send("HSET", msg.Src, "chatroom", ctName)
		err2 := conn.Send("LPUSH", "chatroom:"+ctName, msg.Src)
		if err1 != nil || err2 != nil {
			hazel.SendMessage(daemon.NewSimpleMessage(msg.Src,
				fmt.Sprintf("Failed")))
			return
		}
		hazel.SendMessage(daemon.NewSimpleMessage(msg.Src,
			fmt.Sprintf("you have joined <%s> chatroom", ctName)))
	} else {
		hazel.SendMessage(daemon.NewSimpleMessage(msg.Src,
			fmt.Sprintf("<%s> chatroom is not existed", ctName)))
	}
}