Exemple #1
0
func request(clientObj *client.Client, requestMap map[string]interface{}) {
	b, err := json.Marshal(requestMap)
	if err != nil {
		logUtil.Log(fmt.Sprintf("序列化请求数据%v出错", requestMap), logUtil.Error, true)
	} else {
		clientObj.SendByteMessage(b)
	}
}
Exemple #2
0
// 心跳机制
func heartBeat(clientObj *client.Client, loginSucceedCh chan int) {
	// 处理内部未处理的异常,以免导致主线程退出,从而导致系统崩溃
	defer func() {
		if r := recover(); r != nil {
			logUtil.Log(fmt.Sprintf("通过recover捕捉到的未处理异常:%v", r), logUtil.Error, true)
		}
	}()

	// 先等待登陆成功
	<-loginSucceedCh

	// 将客户端标记为已经登陆
	ClientList[clientObj.Id] = true

	// 随机暂停一段时间
	time.Sleep(time.Duration(mathUtil.GetRandInt(60)) * time.Second)

	// 用于标记是发送心跳包,还是内容
	count := 3

	for {
		count++

		// 每发送4次心跳包,然后发一次包含内容的消息
		if count%4 == 0 {
			// 将count重置为0
			count = 0

			// 发送数据
			requestMap := make(map[string]interface{})
			requestMap = assembleMessageParam(requestMap, "Y")
			request(clientObj, requestMap)
		} else {
			clientObj.SendHeartBeatMessage()
		}

		// 休眠随机时间(单位:秒)
		time.Sleep(HeartBeatInterval * time.Second)
	}
}
Exemple #3
0
// 处理客户端逻辑
func handleClient(clientObj *client.Client, loginSucceedCh chan int) {
	for {
		content, ok := clientObj.GetValieMessage()
		if !ok {
			break
		}

		// 处理数据
		// 1、将结果反序列化
		responseMap := make(map[string]interface{})
		err := json.Unmarshal(content, &responseMap)
		if err != nil {
			logUtil.Log(fmt.Sprintf("反序列化%s出错,错误信息为:%s", string(content), err), logUtil.Error, true)
			continue
		}

		// 2、判断Code是否为0
		code_init, ok := responseMap["Code"].(float64)
		if !ok {
			fmt.Println(fmt.Sprintf("Code:%v,不是int类型", responseMap["Code"]))
			continue
		}
		code := int(code_init)
		if code != 0 {
			// 解析Message
			message, ok := responseMap["Message"].(string)
			if !ok {
				fmt.Println(fmt.Sprintf("Message:%v,不是string类型", responseMap["Message"]))
				continue
			}
			fmt.Println("返回结果不正确,错误信息为:", message)
			continue
		}

		// 3、判断CommandType
		commandType_init, ok := responseMap["CommandType"].(float64)
		if !ok {
			fmt.Println(fmt.Sprintf("CommandType:%v,不是int类型", responseMap["CommandType"]))
			continue
		}
		commandType := int(commandType_init)
		switch commandType {
		case 1: //Login
			loginSucceedCh <- 1
		case 2: //Logout
		case 3: //SendMessage
			// 获取Data
			dataMap, ok := responseMap["Data"].(map[string]interface{})
			if !ok {
				fmt.Println(fmt.Sprintf("Data:%v,不是map[string]interface{}类型", responseMap["Data"]))
				continue
			}

			// 获取Message
			message, ok := dataMap["Message"].(string)
			if !ok {
				fmt.Println(fmt.Sprintf("Message:%v,不是string类型", dataMap["Message"]))
				continue
			}

			// 获取发送者信息:
			fromMap, ok := dataMap["From"].(map[string]interface{})
			if !ok {
				fmt.Println(fmt.Sprintf("From:%v,不是map[string]interface{}类型", dataMap["From"]))
				continue
			}

			// 获取发送者名称:
			name, ok := fromMap["Name"].(string)
			if !ok {
				fmt.Println(fmt.Sprintf("Name:%v,不是string类型", fromMap["Name"]))
				continue
			}

			fmt.Printf("%s说:%s\n", name, message)
		case 4: //UpdatePlayerInfo
		}
	}
}