コード例 #1
0
ファイル: action.go プロジェクト: sctlee/hazel_example
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
ファイル: action.go プロジェクト: sctlee/hazel
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!"))
	}
}
コード例 #3
0
ファイル: action.go プロジェクト: sctlee/hazel
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)))
}
コード例 #4
0
ファイル: action.go プロジェクト: sctlee/hazel_example
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)
}
コード例 #5
0
ファイル: event.go プロジェクト: sctlee/hazel_example
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)
}
コード例 #6
0
ファイル: action.go プロジェクト: sctlee/hazel_example
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)))
	}
}
コード例 #7
0
ファイル: action.go プロジェクト: sctlee/hazel_example
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")
	}
}
コード例 #8
0
ファイル: server.go プロジェクト: sctlee/hazel
func (self *Server) Start() error {
	self.s.Listen(self.config.Port)
	defer self.Close()

	<-self.start
	fmt.Println("server start")

	for {
		conn, err := self.s.Accept()
		if err != nil {
			fmt.Println(err)
			return err
		} else {

			genClientID := func() string {
				self.joinedNumber++
				return fmt.Sprintf("%s.%d", self.config.ServerName, self.joinedNumber)
			}

			cid := genClientID()

			fmt.Println("client id :" + cid)
			tempClient := client.CreateClient(conn, cid)
			self.d.Pending <- tempClient

			go func(c *client.Client) {
				defer func() {
					self.d.Quiting <- c
				}()

				for {
					rawData, ok := c.GetIncoming()
					if !ok {
						break
					}

					fmt.Println(rawData)

					err := self.d.MsgManager.PutMessage(
						message.NewMessage(
							self.config.Pt, string(cid), "", rawData, daemon.MESSAGE_TYPE_TOSERVICE))

					if err != nil {
						fmt.Sprintf("server.error|msg:%s", err)
						self.d.MsgManager.PutMessage(
							daemon.NewSimpleMessage(
								string(cid),
								fmt.Sprintf("server.error|msg:%s", err)))
					}
				}
			}(tempClient)
		}
	}
}
コード例 #9
0
ファイル: action.go プロジェクト: sctlee/hazel
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"))
	}
}
コード例 #10
0
ファイル: action.go プロジェクト: sctlee/hazel
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!"))
	}
}
コード例 #11
0
ファイル: action.go プロジェクト: sctlee/hazel_example
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"))
	}
}