Exemplo n.º 1
0
 /// <summary>
 /// 定时触发重连
 /// </summary>
 private void Reconnect(object sender, ElapsedEventArgs e)
 {
     try
     {
         tmReconnect.Enabled = false;
         DateTime time = DateTime.Now;
         if (preRecTime == null)
         {
             return;
         }
         TimeSpan timeSpan = time - preRecTime;
         if (timeSpan.TotalSeconds > 2)
         {
             this.Invoke((EventHandler)(delegate
             {
                 textScreen.Text = DateTime.Now + ",呼叫盒正在尝试重连至服务";
             }));
             if (client != null)
             {
                 client.Exit();
                 client = null;
             }
             client              = new CallBoxTcpClientServer();
             client.RecvSuccess += RecMes;
             if (client.Setup(config))
             {
                 if (client.Start())
                 {
                     //重连成功,启动心跳
                     if (tmSendHeartBeat != null)
                     {
                         tmSendHeartBeat.Enabled = true;
                     }
                     this.Invoke((EventHandler)(delegate
                     {
                         textScreen.Text = DateTime.Now + ",呼叫盒已重新连接";
                     }));
                 }
             }
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
     finally
     {
         tmReconnect.Enabled = true;
     }
 }
Exemplo n.º 2
0
 /// <summary>
 /// 连接服务器
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void btnConnect_Click(object sender, EventArgs e)
 {
     try
     {
         config.ServerIP = "127.0.0.1";
         config.Port     = 7000;
         if (!client.Setup(config))
         {
             MessageBox.Show("初始化连接失败");
             return;
         }
         bool flag = client.Start();
         if (flag)
         {
             MessageBox.Show("连接成功");
             byteAll.AddRange(new byte[] { 0x00, 0x00 });
             byteAll.AddRange(bytePack);
             if (tmSendHeartBeat == null)
             {
                 tmSendHeartBeat          = new System.Timers.Timer(2000);
                 tmSendHeartBeat.Enabled  = true;
                 tmSendHeartBeat.Elapsed += heartTimesUp;
                 tmReconnect              = new System.Timers.Timer(3000);
                 tmReconnect.Enabled      = true;
                 tmReconnect.Elapsed     += Reconnect;
             }
             textScreen.Text = DateTime.Now + ",呼叫盒连接调度系统成功";
         }
         else
         {
             MessageBox.Show("连接失败");
             return;
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
 }