/// <summary> /// 接收数据 /// </summary> /// <param name="socketclientpara"></param> public void recv(object socketclientpara) { Socket socketClient = socketclientpara as Socket; myJson = new MyJsonCtrl(); while (true) { //创建一个内存缓冲区,其大小为1024*1024字节 即1M byte[] arrServerRecMsg = new byte[1024 * 1024]; //将接收到的信息存入到内存缓冲区,并返回其字节数组的长度 try { int length = socketClient.Receive(arrServerRecMsg); //处理收到的数据 string strSRecMsg = AnalyticData(arrServerRecMsg, length); // Console.WriteLine(strSRecMsg); /// ///zzt重要,这里是解析每个web客户端发过来命令的地方 /// string strReturn = myJson.ReceiceJson(strSRecMsg); SendMessage(socketClient, strReturn); // Console.WriteLine(strReturn); } catch (SocketException ex) { StaticUtils.ShowEventMsg("MgJsonCtrl.class-recv : 接收数据出现异常!\n"); Console.WriteLine("异常抛出" + ex + ",RemoteEnd=" + socketClient.RemoteEndPoint.ToString()); //从通信套接字集合中删除被中断连接的通信套接字 m_listClient.Remove(socketClient.RemoteEndPoint.ToString());//删除异常的端口和IP Thread stopThread = dictThread[socketClient.RemoteEndPoint.ToString()]; //从通信线程中删除被中断的连接通信线程对象 dictThread.Remove(socketClient.RemoteEndPoint.ToString());//删除异常的线程 CrossThreadOperationControl CrossDele = delegate() { for (int j = 0; j < Mainform.form1.listViewIPClient.Items.Count; j++) { if (Mainform.form1.listViewIPClient.Items[j].Text.Equals(socketClient.RemoteEndPoint.ToString())) { Mainform.form1.listViewIPClient.Items.Remove(Mainform.form1.listViewIPClient.Items[j]); j--; } } }; Mainform.form1.listViewIPClient.Invoke(CrossDele); stopThread.Abort(); return; } } }
/// <summary> /// 轮询获取 DB 中App端上报的event /// </summary> public void PollForAppEvent() { /// Design:服务器维护一个关于Event_assemble的DataTable /// 每次轮询更新DataTable ,find a new record and then send a message to clients by websocket; /// Point : if there are many new records found, consist them and the split is signal ','. InitEventTable(); MyJsonCtrl jscontrol = new MyJsonCtrl(); while (true) { refreshEventTable(); string theones = ""; List <string> warning_msg = new List <string>(); //StaticUtils.ShowEventMsg(Convert.ToString(last_PollForAppEvent_time) +" "+ StaticData.g_dtEventAssemble.Rows.Count+"\n"); if (StaticData.g_dtEventAssemble.Rows.Count > 0) { for (int i = 0; i < StaticData.g_dtEventAssemble.Rows.Count; i++) { if (i == StaticData.g_dtEventAssemble.Rows.Count - 1) { theones += StaticData.g_dtEventAssemble.Rows[i]["事件theone"]; } else { theones += StaticData.g_dtEventAssemble.Rows[i]["事件theone"] + ","; } warning_msg.Add("报警时间:" + StaticData.g_dtEventAssemble.Rows[i]["时间"] + " 事件类型:App故障事件上报 事件名称:" + StaticData.g_dtEventAssemble.Rows[i]["事件名称"] + "\n"); } if (!"".Equals(theones)) { //发送数据Json格式 string strSendMSG = jscontrol.EventJson(theones); Mainform.form1.SendMsgToWebSocketClients(strSendMSG); foreach (var item in warning_msg) { //更新UI界面 StaticUtils.ShowEventMsg(item); //计数加一 StaticData.g_inAlarmNum++; } } } Thread.Sleep(5 * 1000); // APP轮询 } }