Example #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)
}
Example #2
0
func (self *AuthAction) SetUserName(msg *message.Message) {
	if !utils.IsExistInMap(msg.Params, "name") {
		hazel.SendMessage(daemon.NewSimpleMessage(msg.Src, "Please input name"))
		return
	}
	name := msg.Params["name"]

	//TODO: set user name

	hazel.SendMessage(daemon.NewSimpleMessage(msg.Src, fmt.Sprintf("Hello, %s", name)))
}
Example #3
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)))
	}
}
Example #4
0
func (self *AuthAction) Signup(msg *message.Message) {
	if !utils.IsExistInMap(msg.Params, "username", "password", "confitm") {
		hazel.SendMessage(daemon.NewSimpleMessage(msg.Src, "msg.Params error"))
		return
	}
	username := msg.Params["username"]
	password := msg.Params["password"]
	confirm := msg.Params["confirm"]

	if strings.EqualFold(password, confirm) {
		user := &UserModel{
			Name:     username,
			Password: password,
		}
		if err := user.Save(); err != nil {
			hazel.SendMessage(daemon.NewSimpleMessage(msg.Src, "Signup error!"))
		} else {
			hazel.SendMessage(daemon.NewSimpleMessage(msg.Src, "Signup success! Now you can login with your account!"))
		}
	} else {
		hazel.SendMessage(daemon.NewSimpleMessage(msg.Src, "confirm is not equal to password"))
	}
}
Example #5
0
func (self *AuthAction) Login(msg *message.Message) {
	/*
		use postgresql
	*/
	if !utils.IsExistInMap(msg.Params, "username", "password") {
		hazel.SendMessage(daemon.NewSimpleMessage(msg.Src, "msg.Params error"))
		return
	}
	username := msg.Params["username"]
	password := msg.Params["password"]

	user, err := Exists(username, password)
	if err != nil {
		hazel.SendMessage(daemon.NewSimpleMessage(msg.Src, "Username or password error!"))
		return
	} else {
		// save login status in msg.Src.sharedPreferences
		// sp := msg.Src.GetSharedPreferences("Auth")
		// sp.Set("username", user.Name)
		self.AuthList[msg.Src] = user.Name
		hazel.SendMessage(daemon.NewSimpleMessage(msg.Src, "Login Success!"))
	}
}
Example #6
0
func (self *ChatroomAction) Send(msg *message.Message) {
	if !utils.IsExistInMap(msg.Params, "msg") {
		hazel.SendMessage(daemon.NewSimpleMessage(msg.Src,
			"Please input msg"))
		return
	}
	conn := db.RedisPool.Get()
	defer conn.Close()
	// if chatroom, ok := self.UserChatList[msg.Src]; ok {
	if ctName, err := redis.String(conn.Do("HGET", msg.Src, "chatroom")); err == nil {
		// self.SendMsg(chatroom, GetUserName(cid), params["msg"])
		cids, err := redis.Strings(conn.Do("LRANGE", "chatroom:"+ctName, "0", "-1"))
		if err != nil {
			fmt.Println("redis error", err)
			return
		}
		self.SendMsg(cids, GetUserName(msg.Src, msg), msg.Params["msg"])
		// self.SendMsg(chatroom, GetUserName(msg, msg.Response), msg.Params["msg"])
	} else {
		hazel.SendMessage(daemon.NewSimpleMessage(msg.Src,
			"You have not joined a chatroom"))
	}
}