func SendMsgController(conn interface{}, msg *protocol.WMessage) { collectionUser := common.DBMongo.C("user") collection := common.DBMongo.C("message") //好友之间消息的转发 //判断两者是不是好友 n, err := collectionUser.Find(bson.M{"user_name": msg.UserInfo.GetUsername(), "friends.name": msg.SendMsg.GetReceiver()}).Count() if err != nil { common.CommonResultReturn(conn, "sendMsgResult", msg.GetMsgTypeId(), 2) Logger.Error("send msg:", err) return } if n != 0 { nowTime := time.Now() log.Println("time:", nowTime) time := nowTime.Unix() msg.SendMsg.MsgTime = &time buffer, err := proto.Marshal(msg) if err != nil { common.CommonResultReturn(conn, "sendMsgResult", msg.GetMsgTypeId(), 2) Logger.Warn("protobuf marshal failed: ", err) return } newbuffer := append(common.IntToBytes(len(buffer)), buffer...) if OnlineCheck(msg.SendMsg.GetReceiver()) { switch (UserMap[msg.SendMsg.GetReceiver()]).(type) { case net.Conn: _, err = (UserMap[msg.SendMsg.GetReceiver()]).(net.Conn).Write(newbuffer) common.CheckErr(err) case websocket.Conn: _, err = (UserMap[msg.SendMsg.GetReceiver()]).(*websocket.Conn).Write(newbuffer) common.CheckErr(err) } } common.CommonResultReturn(conn, "sendMsgResult", msg.GetMsgTypeId(), 0) var message models.Message message.Id = bson.NewObjectId() message.Sender = msg.UserInfo.GetUsername() message.MsgType = int32(msg.SendMsg.GetType()) message.Receiver = msg.SendMsg.GetReceiver() message.Msg = msg.SendMsg.GetMsg() message.MsgTime = nowTime err = collection.Insert(&message) if err != nil { Logger.Error("insert message: ", err) log.Println("message: ", err) } } else { common.CommonResultReturn(conn, "sendMsgResult", msg.GetMsgTypeId(), 1) } }
func GroupMsgController(conn interface{}, msg *protocol.WMessage) { collection := common.DBMongo.C("message") collectionGroup := common.DBMongo.C("group") var groupMsg models.Message var group models.Group nowTime := time.Now() //判断此成员是否在此群组中 n, err := collectionGroup.Find(bson.M{"_id": msg.Group.GetId(), "members.name": msg.UserInfo.GetUsername()}).Count() if err != nil { common.CommonResultReturn(conn, "groupMsgResult", msg.GetMsgTypeId(), 2) Logger.Error("count :", err) return } if n == 0 { //此用户不在此群组中 common.CommonResultReturn(conn, "groupMsgResult", msg.GetMsgTypeId(), 1) return } //保存聊天记录到数据库 groupMsg.Id = bson.NewObjectId() groupMsg.Sender = msg.UserInfo.GetUsername() groupMsg.GroupId = msg.Group.GetId() groupMsg.MsgType = int32(msg.SendMsg.GetType()) groupMsg.Msg = msg.SendMsg.GetMsg() groupMsg.MsgTime = nowTime err = collection.Insert(&groupMsg) if err != nil { Logger.Error("insert group msg:", err) } //转发群组信息给群内所有人 //获取群组员 err = collectionGroup.Find(bson.M{"_id": msg.Group.GetId()}).Select(bson.M{"members": true}).All(&group) if err != nil { Logger.Error("select group member: ", err) common.CommonResultReturn(conn, "groupMsgResult", msg.GetMsgTypeId(), 3) return } common.CommonResultReturn(conn, "groupMsgResult", msg.GetMsgTypeId(), 0) //发送人 user := &protocol.User{} user.Username = &groupMsg.Sender for _, v := range group.Members { //把发送消息的用户排除 if v.Name == msg.UserInfo.GetUsername() { continue } //把群聊信息保存到数据库 var message models.Message message.Id = bson.NewObjectId() message.GroupId = msg.Group.GetId() message.Receiver = v.Name message.MsgType = int32(msg.SendMsg.GetType()) message.MsgTime = nowTime if collection.Insert(&message) != nil { Logger.Error("insert group message:", err) } //如果此成员在线,给其发送消息 if OnlineCheck(v.Name) { sendMsg := &protocol.SendMessage{} sendMsg.Receiver = &v.Name sendMsg.Type = msg.SendMsg.Type sendMsg.Msg = &groupMsg.Msg msgTime := nowTime.Unix() sendMsg.MsgTime = &msgTime returnMsg := &protocol.WMessage{ MsgType: proto.String("groupMsgResult"), //MsgTypeId: proto.Int32(16), StataCode: proto.Int32(0), SendMsg: sendMsg, Group: &protocol.Groups{ Id: proto.String(msg.Group.GetId()), }, UserInfo: user, } buffer, err := proto.Marshal(returnMsg) if err != nil { Logger.Warn("protobuf marshal failed: ", err) continue } newbuffer := append(common.IntToBytes(len(buffer)), buffer...) switch UserMap[v.Name].(type) { case net.Conn: _, err = (UserMap[v.Name]).(net.Conn).Write(newbuffer) common.CheckErr(err) case websocket.Conn: _, err = (UserMap[v.Name]).(*websocket.Conn).Write(newbuffer) common.CheckErr(err) } } } }
//发送好友请求 func SendFirendRequestController(conn interface{}, msg *protocol.WMessage) { collection := common.DBMongo.C("user") nowTime := time.Now() //判断需要添加的好友是否存在 exist, err := collection.Find(bson.M{"user_name": msg.AddFriend.GetReceiver()}).Count() if err != nil { common.CommonResultReturn(conn, "addFriendResult", msg.GetMsgTypeId(), 3) Logger.Error("get friend relation:", err) return } if exist != 0 { //验证是否已经是其好友 n, err := collection.Find(bson.M{"user_name": msg.AddFriend.GetSender(), "friends.name": msg.AddFriend.GetReceiver(), "friends.mode": 3}).Count() if err != nil { common.CommonResultReturn(conn, "addFriendResult", msg.GetMsgTypeId(), 3) Logger.Error("get friend relation:", err) return } //已经是好友关系 if n != 0 { common.CommonResultReturn(conn, "addFriendResult", msg.GetMsgTypeId(), 2) return } //把添加好友信息保存到message表 if int32(msg.AddFriend.GetModes()) == 2 { var saveMsg models.Message saveMsg.Id = bson.NewObjectId() saveMsg.Sender = msg.AddFriend.GetSender() saveMsg.Receiver = msg.AddFriend.GetReceiver() saveMsg.MsgType = int32(msg.AddFriend.GetModes()) saveMsg.Msg = msg.UserInfo.GetUsername() + "请求添加您为好友" saveMsg.MsgTime = nowTime err = collection.Insert(&saveMsg) if err != nil { common.CommonResultReturn(conn, "addFriendResult", msg.GetMsgTypeId(), 3) Logger.Error("add friend request insert database error:", err) return } //把添加好友的信息发给被添加人 if OnlineCheck(msg.AddFriend.GetReceiver()) { num := nowTime.Unix() msg.SendMsg.MsgTime = &num buffer, err := proto.Marshal(msg) if err != nil { common.CommonResultReturn(conn, "addFriendResult", msg.GetMsgTypeId(), 3) Logger.Warn("protobuf marshal failed: ", err) fmt.Println("failed: %s\n", err) //Log.SetLogger("file", `{"filename": "savelog.log"}`) return } length := len(buffer) newbuffer := append(common.IntToBytes(length), buffer...) switch (UserMap[msg.AddFriend.GetReceiver()]).(type) { case net.Conn: _, err = (UserMap[msg.AddFriend.GetReceiver()]).(net.Conn).Write(newbuffer) common.CheckErr(err) case websocket.Conn: _, err = (UserMap[msg.AddFriend.GetReceiver()]).(*websocket.Conn).Write(newbuffer) common.CheckErr(err) } } common.CommonResultReturn(conn, "addFriendResult", msg.GetMsgTypeId(), 0) } else { //发送的数据不正确 common.CommonResultReturn(conn, "addFriendResult", msg.GetMsgTypeId(), 4) } } else { //被添加人不存在 common.CommonResultReturn(conn, "addFriendResult", msg.GetMsgTypeId(), 1) } }