/// <summary> /// 连接中心服务器 /// </summary> /// <returns></returns> private bool ConnectCenterServer() { try { PublicData.RequestSocket = new RequestSocket(); PublicData.RequestSocket.Connect(MQConfig.ResponseServer); MQActionResult <string> result = PublicData.RequestSocket.RequestSendReceive <MQActionResult <string> >( IdKeyData.Create("", "Session/Connect", new ValueTypeParam <ClientTypeEnum>(ClientTypeEnum.Crawl))); if (null != result && result.IsOK) { PublicData.SessionId = result.Data; Log("连接中心服务器成功。"); return(true); } else { MessageBoxEx.Alert("中心服务器连接失败,信息:" + result.ErrorMsg); Log("中心服务器连接失败。"); return(false); } } catch (Exception ex) { MessageBoxEx.Alert(ex.Message); return(false); } }
// DealerReceive数据处理 private void HandleKeyData_DealerReceive(string cmdText, string actionJsonResult) { if (PublicData.ServerParameter == null || PublicData.ServerParameter.IsEmpty()) { MessageBoxEx.Alert($"服务器参数获取失败,无法处理{cmdText}方法"); return; } else { LogUtil.Error($"未处理的DealerReceive消息:cmd:{cmdText}; json:{actionJsonResult}"); } }
/// <summary> /// 发送心跳线程,保持与中心服务器连接 /// </summary> private void ThreadHeartbeat() { int heartbeatErrorCount = 0; while (!PublicData.CancellationToken.IsCancellationRequested && heartbeatErrorCount < 5) { try { MQActionVoidResult result = PublicData.RequestSocket.RequestSendReceive <MQActionVoidResult>( IdKeyData.Create(PublicData.SessionId, "Session/Heartbeat", new HeartbeatParam() { ClientType = ClientTypeEnum.Client })); if (result.IsOK) { heartbeatErrorCount = 0; } else { heartbeatErrorCount++; LogUtil.Warn(Environment.NewLine + "控盘端心跳失败,错误信息:" + result.ErrorMsg); } } catch (Exception ex) { heartbeatErrorCount++; LogUtil.Warn(Environment.NewLine + "控盘端Request发送心跳异常,错误信息:" + Environment.NewLine + ex.StackTrace); } Thread.Sleep(TimeSpan.FromSeconds(2)); } if (heartbeatErrorCount >= 5) { MessageBoxEx.Alert("您已掉线,请关闭重新启动!"); } }
/// <summary> /// 开始执行扫水操作 /// </summary> /// <param name="betNo"></param> private void SendScanWaterToCenterServer(string betNo) { //扫水 //PublicData.RequestSocket = new RequestSocket(); //PublicData.RequestSocket.Connect(MQConfig.ResponseServer); var data = new Collect() { Number = betNo, ClientSessionID = PublicData.SessionId, }; var result = PublicData.RequestSocket.RequestSendReceive <MQActionResult <string> >( IdKeyData.Create(PublicData.SessionId, "Collect/SendCollect", data)); if (result == null) { MessageBoxEx.Alert("连接服务器失败,请检查网络或联系管理员!"); return; } else if (result.IsOK) { //命令发送成功 } }
/// <summary> /// 发送登录请求 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnLogin_Click(object sender, EventArgs e) { // 校验 if (string.IsNullOrWhiteSpace(txtAccount.Text)) { MessageBox.Show("请输入用户名!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (string.IsNullOrWhiteSpace(txtPwd.Text)) { MessageBox.Show("请输入密码!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } var loginParam = new SysMemberLoginParam() { UserName = txtAccount.Text.Trim(), Password = txtPwd.Text.Trim() }; //启动与中心服务器的连接 PublicData.RequestSocket = new RequestSocket(); PublicData.RequestSocket.Connect(MQConfig.ResponseServer); var result = PublicData.RequestSocket.RequestSendReceive <MQActionResult <string> >( IdKeyData.Create(PublicData.SessionId, "SysMember/Login", loginParam)); if (result == null) { MessageBoxEx.Alert("连接服务器失败,请检查网络或联系管理员!"); return; } else if (result.IsOK) { PublicData.SessionId = result.Data; PublicData.SysmemberAccount = loginParam.UserName; Visible = false; using (var formLogin = new MainForm() { StartPosition = FormStartPosition.CenterScreen, TopMost = true }) { //暂时采用模式调试 formLogin.Show(); //var diaResult = formLogin.ShowDialog(); //if (diaResult == DialogResult.Cancel) //{ // Close(); // return; //} //else if (formLogin.ShowDialog() == DialogResult.Ignore)//重新登录 //{ // Visible = true; // return; //} } } else { MessageBoxEx.Alert(result.ErrorMsg); txtPwd.Focus(); return; } }