云信批量发送消息

        // 存放定时器,避免被回收
        static System.Threading.Timer[] timers;

        // 记录批次
        public static int ICOUNT = 0;
        static StringBuilder sbError = new StringBuilder();

        public static void Send()
        {
            #region 批量发送测试 每分钟最多120次,每次最多向500人发送
            // 读取接收方accid
            using (FileStream fs = File.OpenRead("ACCID_20180608.txt"))
            {
                using (StreamReader sr = new StreamReader(fs))
                {
                    string str = sr.ReadToEnd();
                    if (String.IsNullOrWhiteSpace(str))
                    {
                        // 无接收方
                        return;
                    }
                    string[] strs = str.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

                    // 批次,每批最多500
                    int iBatch = (int)Math.Ceiling(strs.Length / 500.00);

                    // 切片存储 [单次][500用户]
                    string[][] arr = new string[iBatch][];
                    for (int i = 1; i <= iBatch; i++)
                    {
                        arr[i - 1] = strs.Skip((i - 1) * 500).Take(500).ToArray();
                    }

                    // [批次][单次][500用户] 每批 120*500
                    int iTarr = (int)Math.Ceiling(arr.Length / 120.00);
                    string[][][] tarr = new string[iTarr][][];

                    timers = new System.Threading.Timer[iTarr];
                    for (int i = 1; i <= iTarr; i++)
                    {
                        int index = i - 1;
                        // 每批次传送的数据
                        tarr[index] = arr.Skip(index * 120).Take(120).ToArray();

                        // 第一批立即执行,之后每70秒执行下一批 每批次只执行一次
                        timers[index] = new System.Threading.Timer(new System.Threading.TimerCallback(Send120), tarr[index], TimeSpan.FromSeconds(index * 70), TimeSpan.FromMinutes(0));
                    }
                }
            }
            #endregion
        }
// 调用接口,发送消息 public static void Send120(object state) { ++ICOUNT; Console.WriteLine($"----------------- {ICOUNT} ---------------------"); // 用户ID列表 string[][] arr = state as string[][]; // 每分钟最多120个请求 for (int i = 0; i < (arr.Length>120?120:arr.Length); i++) { Task<string> task = new Task<string>((o) => { dynamic d = o as dynamic; // 发送消息接口 string url = " https://api.netease.im/nimserver/msg/sendBatchMsg.action"; string param = string.Format("fromAccid={0}&toAccids={1}&type=0&body={2}", "发送方账号", JsonConvert.SerializeObject(d.data), JsonConvert.SerializeObject(new { msg = "要发送的消息!" })); // 费时任务 string sResult= IM_HttpPost(url, param); dynamic dResult = JsonConvert.DeserializeObject<dynamic>(sResult); if (dResult.code != "200") { // 记录出错的数据 sbError.Append(string.Join("\n\r", d.data)); } return dResult.code; }, new { index = i, data = arr[i] }); task.ContinueWith((t, o) => { Console.WriteLine($"次数:{o},返回值:{t.Result}"); }, i); task.Start(); } }

 

posted @ 2018-06-13 10:52  Coder_小菜  阅读(309)  评论(0编辑  收藏  举报