// 处理讨论组消息转发 func HandleGroupTextChat(conn *net.TCPConn, recPacket *packet.Packet) { // read readMsg := &pb.PbGroupTextChat{} packet.Unpack(recPacket, readMsg) from_uuid := ConnMapUuid.Get(conn).(string) group_id := readMsg.GetGroupId() txt_msg := readMsg.GetTextMsg() // timestamp := readMsg.GetTimestamp() // 验证发送者,这样会影响性能,视情况可以把这个验证去掉 if readMsg.GetFromUuid() != from_uuid || !groupinfo.ExistUuidFromGroup(group_id, from_uuid) { CloseConn(conn) return } // write writeMsg := &pb.PbGroupTextChat{ FromUuid: proto.String(from_uuid), GroupId: proto.String(group_id), TextMsg: proto.String(txt_msg), Timestamp: proto.Int64(time.Now().Unix()), } // 在线消息包 pac1, err := packet.Pack(packet.PK_GroupTextChat, writeMsg) if err != nil { log.Printf("%v\r\n", err) return } // 离线消息包 pac2, err := packet.Pack(packet.PK_ServerResponseGroupOfflineMsg, writeMsg) if err != nil { log.Printf("%v\r\n", err) return } // 将消息转发给所有组员(除了自己),不在线则离线存储 group_members := groupinfo.GetAllUuid(group_id) for i := 1; i < len(group_members); i++ { if group_members[i] == from_uuid { continue } if cid.UuidCheckOnline(group_members[i]) { to_conn := UuidMapConn.Get(group_members[i]).(*net.TCPConn) SendByteStream(to_conn, pac1.GetBytes()) } else { groupmsg.AddMsg(group_members[i], string(pac2.GetBytes())) } } }
// 处理C2C消息转发 func HandleC2CTextChat(conn *net.TCPConn, recPacket *packet.Packet) { // read readMsg := &pb.PbC2CTextChat{} packet.Unpack(recPacket, readMsg) from_uuid := ConnMapUuid.Get(conn).(string) to_uuid := readMsg.GetToUuid() txt_msg := readMsg.GetTextMsg() // timestamp := readMsg.GetTimestamp() // 验证发送者的真实性以及发送对象是否存,若消息伪造,则断开该连接 if readMsg.GetFromUuid() != from_uuid || !cid.UuidCheckExist(to_uuid) { CloseConn(conn) return } // write writeMsg := &pb.PbC2CTextChat{ FromUuid: proto.String(from_uuid), ToUuid: proto.String(to_uuid), TextMsg: proto.String(txt_msg), Timestamp: proto.Int64(time.Now().Unix()), } // 在线消息包 pac1, err := packet.Pack(packet.PK_C2CTextChat, writeMsg) if err != nil { log.Printf("%v\r\n", err) return } // 离线消息包 pac2, err := packet.Pack(packet.PK_ServerResponseC2COfflineMsg, writeMsg) if err != nil { log.Printf("%v\r\n", err) return } // 若 to_uuid 在线,则转发该消息,发送失败 或者 to_uuid不在线 则保存为离线消息 if cid.UuidCheckOnline(to_uuid) { to_conn := UuidMapConn.Get(to_uuid).(*net.TCPConn) if SendByteStream(to_conn, pac1.GetBytes()) != nil { c2cmsg.AddMsg(to_uuid, string(pac2.GetBytes())) report.AddCount(report.OfflineMsg, 1) } else { report.AddCount(report.OnlineMsg, 1) } } else { c2cmsg.AddMsg(to_uuid, string(pac2.GetBytes())) report.AddCount(report.OfflineMsg, 1) } }
// 发送protobuf结构数据 func SendPbData(conn *net.TCPConn, dataType uint32, pb interface{}) error { pac, err := packet.Pack(dataType, pb) if err != nil { return err } return SendByteStream(conn, pac.GetBytes()) }
// 处理客户端之间的消息转发 func HandleC2CTextChat(conn *net.TCPConn, recPacket *packet.Packet) { // read readMsg := &pb.PbC2CTextChat{} packet.Unpack(recPacket, readMsg) from_uuid := ConnMapUuid.Get(conn).(string) to_uuid := readMsg.GetToUuid() txt_msg := readMsg.GetTextMsg() timestamp := readMsg.GetTimestamp() // 验证发送者的真实性以及发送对象是否存,若消息伪造,则断开该连接 if readMsg.GetFromUuid() != from_uuid || !dao.UuidCheckExist(to_uuid) { CloseConn(conn) return } // write writeMsg := &pb.PbC2CTextChat{ FromUuid: proto.String(from_uuid), ToUuid: proto.String(to_uuid), TextMsg: proto.String(txt_msg), Timestamp: proto.Int64(timestamp), } pac, err := packet.Pack(packet.PK_C2CTextChat, writeMsg) if err != nil { log.Printf("%v\r\n", err) return } // 若 to_uuid 在线,则转发该消息,发送失败 或者 to_uuid不在线 则保存为离线消息 if dao.UuidCheckOnline(to_uuid) { // fmt.Println("在线消息转发") to_conn := UuidMapConn.Get(to_uuid).(*net.TCPConn) if SendByteStream(to_conn, pac.GetBytes()) != nil { // fmt.Println("发送失败转离线消息保存") dao.OfflineMsgAddMsg(to_uuid, string(pac.GetBytes())) report.AddCount(report.OfflineMsg, 1) } else { report.AddCount(report.OnlineMsg, 1) } } else { // fmt.Println("不在线转离线消息保存") dao.OfflineMsgAddMsg(to_uuid, string(pac.GetBytes())) report.AddCount(report.OfflineMsg, 1) } }