コード例 #1
0
        // 发送群聊消息
        public static void SendGroupWord(string word, string destId)    // destId 群Id
        {
            AddressBook.Item targetItem = null;
            foreach (var item in App.addressBook.items)
            {
                if (!item.isGroup)
                {
                    continue;
                }
                if (item.UserName == destId)
                {
                    targetItem = item;
                    break;
                }
            }
            if (targetItem == null)
            {
                MessageBox.Show("您不属于当前群聊");
                return;
            }
            // 因为群聊中可能存在陌生人,重新向服务器查询并发送
            foreach (var item in targetItem.GroupUserName)
            {
                if (item == App.user.userName)
                {
                    continue;
                }
                string recv = CSClient.getInstance().SendAMsg($"q{item}");
                switch (recv)
                {
                case "error":
                    MessageBox.Show("服务器查询错误");
                    continue;

                case "Please send the correct message.":
                    MessageBox.Show("群聊中有未知用户");
                    continue;

                case "n":
                    continue;

                default:
                    break;
                }
                string groupId      = destId;
                string singleUserId = item;
                byte[] data         = AppProtocol.PackWord($"from:{App.user.userName}\n{word}", groupId, singleUserId);
                P2PSender.getInstance().SendData(data, recv, P2PListener.GENERALLISTENPORT);
            }
        }
コード例 #2
0
        // 新群组
        // 输入群成员id列表(分号分割),给所有相关人员发送NewGroupp控制报文
        public static void newGroup(string userNameList)
        {
            string groupId  = $"g{new Random().Next(0, 1000)}";
            string srcId    = groupId;
            string optional = App.user.userName + ";" + userNameList;

            string[] destIDList = userNameList.Split(';');
            foreach (var item in destIDList)
            {
                string recv = CSClient.getInstance().SendAMsg($"q{item}");
                switch (recv)
                {
                case "error":
                    MessageBox.Show("服务器查询错误");
                    continue;

                case "Please send the correct message.":
                    MessageBox.Show("群聊中有未知用户");
                    continue;

                case "n":
                    continue;

                default:
                    break;
                }
                string destId = item;
                byte[] data   = AppProtocol.PackControl(AppProtocol.ControlType.NEWGROUP, srcId, destId, optional);
                P2PSender.getInstance().SendData(data, recv, P2PListener.GENERALLISTENPORT);
            }

            App.addressBook.items.Add(new Model.AddressBook.Item()
            {
                UserName = srcId, GroupUserName = optional.Split(';'), Alias = srcId, isGroup = true
            });
        }