public static bool PostMessage(string cmd) { if (client == null) { client = new QcMsgClient(null, null); client.ConnectedServer += (o, e) => { client.Send(cmd); }; client.Connect(QcMessagner.IP, QcMessagner.Port); } else { try { client.Send(cmd); } catch (Exception e) { QcLog.LogString("PostMessagee" + e.Message); client = null; PostMessage(cmd); } } return(true); }
//System.Timers.Timer m_Timer; private void ConnectServer() { try { if (client == null) { client = new QcMsgClient(QcUser.Users, QcUser.User); client.ConnectedServer += (o, e) => { client.Send(QcCmd.MakeCmd(QcProtocol.QcCommand.QcUserLogin, User.Name, "*")); //client.Send(QcCmd.MakeCmd(QcProtocol.QcCommand.QcLoginOut, "*", User.Name)); }; client.DataUpdate += (o, e) => { if (this.DataUpdate != null) { this.DataUpdate(o, e); } }; client.DisConnectedServer += (o, e) => { client.Send(QcCmd.MakeCmd(QcProtocol.QcCommand.QcLoginOut, User.Name, "*")); }; client.ReceiveCmd += QcMessagner_ReciveCmd; } client.Connect(ip, port); } catch (Exception ex) { QcLog.LogException("MessagnerCS", ex); } }
LicenseRetCode PostLicenseMessage(string type, string arg) { int count = 3; QcMsgClient client = new QcMsgClient(null, null); AutoResetEvent are = new AutoResetEvent(false); QcCmd retcmd = null; string cmd = QcCmd.MakeCmd(QcProtocol.QcCommand.QcCheckLicense, type, arg); try { //client =; client.ConnectedServer += (o, e) => { client.Send(cmd); }; client.ReceiveCmd += (o, e) => { retcmd = e.Cmd; are.Set(); //接收消息 }; CONTINUE: client.Connect(QcMessagner.IP, QcMessagner.Port); if (are.WaitOne(5000))//3秒钟超时连接 { LicenseRetCode ret = LicenseRetCode.授权未知错误01; //if (retcmd == null) //{ // Thread.Sleep(500);//有可能消息接收 事件还没有给retcmd赋值,因此延迟500毫秒 //} if (retcmd != null) { string t_Test = retcmd.tokens(1); ret = (LicenseRetCode)Enum.Parse(typeof(LicenseRetCode), t_Test); } return(ret); } else { if (count > 0) { count--; goto CONTINUE; } else { return(LicenseRetCode.授权服务器连接超时); } } } catch (Exception e) { QcLog.LogString(e.Message + e.StackTrace); //System.Windows.Forms.MessageBox.Show(e.Message + e.StackTrace); return(LicenseRetCode.授权未知错误); } finally { //这里该怎样断开服务器上的连接呢? if (client != null) { client.Close(); } are.Set(); } }