Beispiel #1
0
func ReciveResult(conn net.Conn, cn chan int) {
	const MAXLEN = 1024
	buf := make([]byte, MAXLEN)

	for true {
		n, _ := conn.Read(buf) //接收具体消息
		//接收数据包
		//接收包的type类型用来区分包之间的区别
		typeStruct := new(protocol.S2SSystem_GetType)
		if err := proto.Unmarshal(buf[0:n], typeStruct); err != nil {
			CheckError(err)
			return
		}

		switch *typeStruct.Type {
		case 5: //接收返回结果ResultInfo
			result := new(protocol.S2SSystem_ResultInfo)
			if err := proto.Unmarshal(buf[0:n], result); err == nil {
				switch result.GetResult() {
				case 5:
					fmt.Println("login sccessfull!")
					ok = true

					request := &protocol.S2SSystem_Request{
						Type:   proto.Int32(3),
						Result: proto.Int32(1),
					}
					encObj, _ := proto.Marshal(request)
					conn.Write(encObj)
					break
				}
			}
			break
		case 4: //ResultChatMsg
			result := new(protocol.S2SSystem_ResultChatMsg)
			if err := proto.Unmarshal(buf[0:n], result); err == nil {
				for i := 0; i < len(result.Type); i++ {
					fmt.Println(result.Playername[i], "say:", result.Msg[i])
				}
			}
			break
		}
	}

}