示例#1
0
// 客户端请求获取讨论组信息
func HandleClientRequestGroupInfo(conn *net.TCPConn, recPacket *packet.Packet) {
	readMsg := &pb.PbClientRequestGroupInfo{}
	packet.Unpack(recPacket, readMsg)

	uuid := readMsg.GetFromUuid()
	// timestamp := readMsg.GetTimestamp()

	groupids := groupinfo.GetAllGroup(uuid)

	var allGroupInfo []*pb.PbGroupInfo

	for _, group_id := range groupids {
		s := groupinfo.GetAllUuid(group_id)
		group_name, owner_uuid, member_uuid := s[0], s[1], s[2:]

		gi := &pb.PbGroupInfo{
			GroupId:    proto.String(group_id),
			GroupName:  proto.String(group_name),
			OwnerUuid:  proto.String(owner_uuid),
			MemberUuid: member_uuid,
		}

		allGroupInfo = append(allGroupInfo, gi)
	}

	// write
	writeMsg := &pb.PbServerResponseGroupInfo{
		FromUuid:     proto.String(uuid),
		AllGroupInfo: allGroupInfo,
		Timestamp:    proto.Int64(time.Now().Unix()),
	}
	SendPbData(conn, packet.PK_ServerResponseGroupInfo, writeMsg)
}
示例#2
0
// 客户端申请加入讨论组
func HandleClientJoinGroup(conn *net.TCPConn, recPacket *packet.Packet) {
	// read
	readMsg := &pb.PbClientJoinGroup{}
	packet.Unpack(recPacket, readMsg)

	from_uuid := readMsg.GetFromUuid()
	group_id := readMsg.GetGroupId()
	// note_msg := readMsg.GetNoteMsg()
	// timestamp := readMsg.GetTimestamp()

	// 加入讨论组
	if ret := groupinfo.JoinGroup(from_uuid, group_id); !ret {
		return
	}

	group_name, _ := groupinfo.GetGroupNameAndOwner(group_id)

	// write
	writeMsg := pb.PbServerNotifyJoinGroup{
		ApplicantUuid: proto.String(from_uuid),
		GroupId:       proto.String(group_id),
		GroupName:     proto.String(group_name),
		Timestamp:     proto.Int64(time.Now().Unix()),
	}

	// 通知所有组员,这个消息不离线存储
	group_members := groupinfo.GetAllUuid(group_id)
	for i := 1; i < len(group_members); i++ {
		SendPbData(UuidMapConn.Get(group_members[i]).(*net.TCPConn), packet.PK_ServerNotifyJoinGroup, writeMsg)
	}
}
示例#3
0
// 处理讨论组消息转发
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()))
		}
	}

}