func (co *UserManager) Input(userMap map[string]chan *lm.Data_Message, user *lm.Data_User) { defer co.Conn.Close() defer delete(userMap, user.GetUser()) //// del for { size := make([]byte, 4) index, err := co.Conn.Read(size) if err != nil { fmt.Println(err.Error()) return } if index > 0 { size_ := int32(binary.BigEndian.Uint32(size)) data := make([]byte, size_) dataObj := new(lm.Data) co.Conn.Read(data) proto.Unmarshal(data, dataObj) println("========= receive start %d", *dataObj.Type) if *dataObj.Type == int32(lm.Data_MESSAGE) { println("========= receive msg") msg := dataObj.GetMsg() msg.Send = new(string) *msg.Send = user.GetUser() //TODO println(*msg.Send + "========= receive send") accept := userMap[msg.GetAccept()] if accept != nil { println("========= receive ok", msg.GetAccept()) userMap[msg.GetAccept()] <- msg } println(*msg.Send + "========= receive ok") co.ResultOK() ////接收信息OK } } } }
func (co *UserManager) Output(userMap map[string]chan *lm.Data_Message, user *lm.Data_User) { defer co.Conn.Close() defer delete(userMap, user.GetUser()) //// del for { msg := <-userMap[user.GetUser()] println(user.GetUser(), "======= output") outData := new(lm.Data) outData.Version = new(int32) outData.Type = new(int32) outData.Time = new(uint64) *outData.Version = 1 *outData.Type = int32(lm.Data_MESSAGE) *outData.Time = 111111 outData.Msg = msg /////// data, _ := proto.Marshal(outData) dataSize := make([]byte, 4) binary.BigEndian.PutUint32(dataSize, uint32(len(data))) ////TODO data的长度要做限制,要不然会出现计数不准的情况 co.Conn.Write(dataSize) co.Conn.Write(data) } }