zoukankan      html  css  js  c++  java
  • 云信批量发送消息

            // 存放定时器,避免被回收
            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(" ", d.data)); } return dResult.code; }, new { index = i, data = arr[i] }); task.ContinueWith((t, o) => { Console.WriteLine($"次数:{o},返回值:{t.Result}"); }, i); task.Start(); } }
  • 相关阅读:
    【数位dp】Beautiful Numbers @2018acm上海大都会赛J
    【状压dp】Trie 树 @中山纪念中学20170304
    两个给点染色的问题-树上染色与图上染色
    【贪心】经营与开发 @upc_exam_5500
    【二分+拓扑排序】Milking Order @USACO 2018 US Open Contest, Gold/upc_exam_6348
    【并查集】Connectivity @ABC049&amp;ARC065/upcexam6492
    【倍增】T-shirt @2018acm徐州邀请赛 I
    Sparse Coding: Autoencoder Interpretation
    Sparse Coding
    Pooling
  • 原文地址:https://www.cnblogs.com/coder-soldier/p/9176604.html
Copyright © 2011-2022 走看看