public static async Task RunClientAsync(BodyStrongMessage msg) { var group = new MultithreadEventLoopGroup(); try { var bootstrap = new Bootstrap(); bootstrap .Group(group) .Channel <TcpSocketChannel>() .Option(ChannelOption.TcpNodelay, true) .Handler(new ActionChannelInitializer <ISocketChannel>(channel => { IChannelPipeline pipeline = channel.Pipeline; pipeline.AddLast("frameDecoder", new ProtobufVarint32FrameDecoder()); pipeline.AddLast("decoder", new ProtobufDecoder(BodyStrongMessage.Parser)); pipeline.AddLast("frameEncoder", new ProtobufVarint32LengthFieldPrepender()); pipeline.AddLast("encoder", new ProtobufEncoder()); pipeline.AddLast("tcpHandler", new HeartbeatHandler()); })); IChannel bootstrapChannel = await bootstrap.ConnectAsync(new IPEndPoint(IPAddress.Parse("192.168.43.100"), 29999)); await bootstrapChannel.WriteAndFlushAsync(msg); //Console.ReadLine(); await bootstrapChannel.CloseAsync(); } finally { group.ShutdownGracefullyAsync().Wait(1000); } }
/// <summary> /// 应用启动的时候生命周期 /// </summary> /// <param name="ex"></param> protected override void OnStartup(StartupEventArgs ex) { //全局异常处理机制,UI异常 Current.DispatcherUnhandledException += App_OnDispatcherUnhandledException; //全局异常处理机制,线程异常 AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; //加载语言 LanguageUtils.SetLanguage(); //启动netty,用于与设备端通信 Thread th = new Thread(() => { try { logger.Info("线程启动成功"); NettyLuncher.getInstance().Start().Wait(); } catch (AggregateException ee) { App.Current.Dispatcher.Invoke(new Action(() => { MessageBoxX.Info(LanguageUtils.GetCurrentLanuageStrByKey("App.PortOccupy")); System.Environment.Exit(0); })); } }); th.Start(); //启动远程更新 Thread updateTh = new Thread(() => { try { Thread.Sleep(1000 * 8); Dictionary <string, string> param = new Dictionary <string, string>(); param.Add("version", CommUtil.GetCurrentVersion()); var result = HttpSender.GET(HttpSender.URL_UPDATE, param); if (string.IsNullOrEmpty(result)) { return; } var info = JsonTools.DeserializeJsonToObject <VersionInfo>(result); if (info == null || info.update == false) { return; } info.language = LanguageUtils.IsChainese() ? 1 : 0; App.Current.Dispatcher.Invoke(new Action(() => { Boolean dr = MessageBoxX.Question(LanguageUtils.GetCurrentLanuageStrByKey("App.UpdateInfo")); if (dr == true) { Process.Start("AutoUpdater.exe", info.GetProcessString()); Environment.Exit(0); } })); } catch (Exception exc) { Console.WriteLine(exc.ToString()); return; } }); updateTh.Start(); //大数据线程 Thread bdth = new Thread(() => { try { SetterDAO setterDao = new SetterDAO(); AuthDAO authDAO = new AuthDAO(); while (true) { if (setterDao.ListAll().Count != 1) { //不激活不开启 Thread.Sleep(1000 * 15); continue; } if (authDAO.ListAll().Count == 1) { //Console.WriteLine("-----------------boom shakalaka--------------"); //只有admin,不创建用户,不开启,睡个15s Thread.Sleep(1000 * 15); continue; } try { BigDataOfficer bigDataOfficer = new BigDataOfficer(); bigDataOfficer.Run(); //int heartBeatRate = (int)CommUtil.GetBigDataRate(); Thread.Sleep(1000 * 60); //Console.WriteLine("-----------------boom"); } catch (Exception e) { logger.Error("大数据线程失败:" + e.StackTrace); Console.WriteLine("大数据线程失败:" + e.Message); } } } catch (Exception exct) { Console.WriteLine(exct.ToString()); } }); bdth.Start(); //心跳线程 Thread hbth = new Thread(() => { ProtoBufSocket socket = null; try { socket = new ProtoBufSocket(); socket.Connect(); } catch (Exception exception) { Console.WriteLine("连接失败:" + exception.StackTrace); TcpHeartBeatUtils.WriteLogFile("连接失败:" + exception.StackTrace); } while (true) { try { BodyStrongMessage bodyStrongMessage = new BodyStrongMessage { MessageType = BodyStrongMessage.Types.MessageType.Heaerbeatreq, //可能为null HeartbeatRequest = TcpHeartBeatUtils.GetHeartBeatByCurrent() }; socket.SendMessage(bodyStrongMessage); Console.WriteLine("发送msg!!"); //Thread.Sleep(5000); } catch (Exception eee) { Console.WriteLine("发送msg失败" + eee.StackTrace); TcpHeartBeatUtils.WriteLogFile("发送msg失败" + eee.StackTrace); } finally { Thread.Sleep(5000); } } }); hbth.Start(); base.OnStartup(ex); }