Esempio n. 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)
	}
}
Esempio n. 2
0
// 心跳机制
func heartBeat(ch chan int, clientObj *client.Client) {
	// 处理内部未处理的异常,以免导致主线程退出,从而导致系统崩溃
	defer func() {
		if r := recover(); r != nil {
			logUtil.Log(fmt.Sprintf("通过recover捕捉到的未处理异常:%v", r), logUtil.Error, true)
		}

		ch <- 1
	}()

	for {
		clientObj.SendHeartBeatMessage()

		// 休眠指定的时间(单位:秒)
		time.Sleep(HeartBeatInterval * time.Second)
	}
}
Esempio n. 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
			fmt.Println("登陆成功,可以发送信息")
			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
			}

			// 判断是不是系统消息
			var name string
			var fromMap map[string]interface{}
			if name, ok = dataMap["From"].(string); ok {

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

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