Esempio n. 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)
}
Esempio n. 2
0
func (self *AuthAction) Logout(msg *message.Message) {
	// sp := msg.Src.GetSharedPreferences("Auth")
	// if _, ok := sp.Get("name"); ok {
	// sp.Del("name")
	if _, ok := self.AuthList[msg.Src]; ok {
		delete(self.AuthList, msg.Src)
		hazel.SendMessage(daemon.NewSimpleMessage(msg.Src, "Logout success!"))
	} else {
		hazel.SendMessage(daemon.NewSimpleMessage(msg.Src, "Please login first!"))
	}
}
Esempio n. 3
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)))
}
Esempio n. 4
0
func (self *ChatroomAction) List(msg *message.Message) {
	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)
		hazel.SendMessage(daemon.NewSimpleMessage(msg.Src,
			"service error"))
	}
	fmt.Printf("%t", chatrooms)
	response := daemon.NewSimpleMessage(msg.Src,
		fmt.Sprintf("You can choose one chatroom to join:\n%s",
			strings.Join(chatrooms, "\t")))
	//original way: CHATROOMS -> chatrooms
	hazel.SendMessage(response)
}
Esempio n. 5
0
func (self *AuthAction) GetUserName(msg *message.Message) {
	if username, ok := self.AuthList[msg.Params["cid"]]; ok {
		msg.Params["username"] = username
	} else {
		msg.Params["username"] = "******"
	}
	msg.Des = msg.Src
	hazel.SendMessage(msg)
}
Esempio n. 6
0
func (self *ChatroomAction) OnClientQuit(cid string) {
	msg := daemon.NewSimpleMessage("", "")
	msg.Src = cid
	msg.Des = "chatroom"
	msg.Command = "exit"
	msg.Params = make(map[string]string)
	msg.Type = daemon.MESSAGE_TYPE_TOSERVICE
	hazel.SendMessage(msg)
}
Esempio n. 7
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)))
	}
}
Esempio n. 8
0
// original way
// func (self *ChatroomAction) SendMsg(chatroom *Chatroom, username string, msg string) {
// now use redis
func (self *ChatroomAction) SendMsg(cids []string, username string, msg string) {
	// fatalCids := []string{"1.0", "0.0"}
	response := daemon.NewSimpleBoardMessage(
		cids,
		fmt.Sprintf("%s says: %s",
			username,
			msg))
	hazel.SendMessage(response)

}
Esempio n. 9
0
func (self *ChatroomAction) Exit(msg *message.Message) {
	conn := db.RedisPool.Get()
	defer conn.Close()

	/*	original way:
		delete cid in chatroom.cids by manual delete from slice
	*/
	// if chatroom, ok := self.UserChatList[msg.Src]; ok {
	// for i, c := range chatroom.cids {
	// 	if c == msg.Src {
	// 		chatroom.cids = append(chatroom.cids[:i],
	// 			chatroom.cids[i+1:]...)
	// 		// cid.PutOutgoing(fmt.Sprintf("you have exited <%s> chatroom", chatroom.ct.Name))
	// 		break
	// 	}
	// }
	// delete(self.UserChatList, msg.Src)

	/*	now use redis:
		delete easily
	*/
	exists, err := redis.Bool(conn.Do("HEXISTS", msg.Src, "chatroom"))
	if err != nil {
		fmt.Println("redis err:", err)
		return
	}
	if exists {
		ctName, err := redis.String(conn.Do("HGET", msg.Src, "chatroom"))
		err = conn.Send("LREM", "chatroom:"+ctName, "0", msg.Src)
		err = conn.Send("HDEL", msg.Src, "chatroom")
		cids, err := redis.Strings(conn.Do("LRANGE", "chatroom:"+ctName, "0", "-1"))
		if err != nil {
			fmt.Println("redis error:", err)
			hazel.SendMessage(daemon.NewSimpleMessage(msg.Src,
				fmt.Sprintf("Failed")))
			return
		}
		hazel.SendMessage(daemon.NewSimpleMessage(msg.Src,
			fmt.Sprintf("Quited Success")))

		self.SendMsg(cids, GetUserName(msg.Src, msg), "has exited")
	}
}
Esempio n. 10
0
func GetUserName(cid string, originalMsg *message.Message) (username string) {
	request := message.NewMessage(nil, "chatroom", "auth",
		map[string]string{"command": "getusername", "cid": cid},
		daemon.MESSAGE_TYPE_TOSERVICE)
	message.CopySession(originalMsg, request)
	hazel.SendMessage(request)
	select {
	case msg := <-request.Response:
		return msg.Params["username"]
	case <-time.After(time.Second * 2):
		return "匿名(auth time out)"
	}
}
Esempio n. 11
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"))
	}
}
Esempio n. 12
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!"))
	}
}
Esempio n. 13
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"))
	}
}