public static readonly string msgHubServer = "https://msghub.rupeng.com/";//"http://*****:*****@888_6xx!aa"); ViewBag.token = token; return(View()); }
public async Task <IActionResult> Index() { var httpClient = httpClientFactory.CreateClient(); MsgHubClient client = new MsgHubClient(msgHubServer, httpClientFactory); var token = await client.GetTokenAsync("1", "yzk", "rupenggongkaike", DateTime.Now.ToFileTime(), "wpstt@999_6xx!aa"); ViewBag.token = token; return(View()); }
static async Task StartOneClientAsync(int taskId) { var token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJBcHBLZXkiOiJydXBlbmdnb25na2Fpa2UiLCJEaXNwbGF5TmFtZSI6IuadqOS4reenkSIsIlVzZXJJZCI6IjI4IiwiZXhwIjoxNTU3NjcxMDI5LCJpc3MiOiJtZSIsImF1ZCI6InlvdSJ9.Xa9hmoMbCRzwIyGvnUX7L4GSAXhVPUsi2jLz4Kvj2mk"; var chatRoomId = "Activity_72"; var connection = new HubConnectionBuilder() .WithUrl("https://msghub.rupeng.com/messageHub", options => { options.AccessTokenProvider = () => Task.FromResult(token); }) .Build(); connection.Closed += async(error) => { Console.WriteLine("连接断开,尝试重连"); await Task.Delay(2000); await connection.StartAsync(); }; try { await connection.StartAsync(); await connection.InvokeAsync("GetGroupMessages", chatRoomId); MsgHubClient client = new MsgHubClient("https://msghub.rupeng.com/", httpClientFactory); string sdkToken = await client.GetTokenAsync("28", "yzk", appkey, DateTime.Now.ToFileTime(), appsecret); Random rand = new Random(); for (; ;) { try { await client.SendGroupMessageAsync(token, chatRoomId, "txtMsg", "[" + Dns.GetHostName() + "]-" + Guid.NewGuid()); Console.WriteLine("发送消息完成,taskId=" + taskId); await Task.Delay(rand.Next(20000, 50000));//不能用Thread.Sleep } catch (Exception ex) { Console.WriteLine("发送消息失败, taskId = " + taskId + "," + ex); } } } catch (Exception ex) { Console.WriteLine($"启动任务失败,taskId=${taskId},${ex}"); } }