/// <summary> /// 有设备改变运行状态 /// </summary> /// <param name="token"></param> /// <param name="state"></param> /// <param name="callbackId"></param> /// <param name="data"></param> public static void OnDeviceChangeState(string token, int state, string callbackId, byte[] data) { if (state == 203) { string stateDrive = ClassStatic.GetString(data); if (ClassStatic.clientList.ContainsKey(token)) { ClassStatic.Client client = ClassStatic.clientList[token]; client.state = (stateDrive == "在线" ? 0 : 1); ClassStatic.clientList[token] = client; if (ClassStatic.clientOnlineList.ContainsKey(client.uid)) { List <string> clientOnlineList = ClassStatic.clientOnlineList[client.uid]; foreach (string tokenTemp in clientOnlineList) { if (tokenTemp != token) { ClassStatic.tcpServer.Send(tokenTemp, 203, ClassStatic.GetBytes(token + (client.state == 1 ? "勿扰" : "在线"))); System.Diagnostics.Debug.WriteLine("发送改变到客户端:" + tokenTemp); } } } } // 向客户端返回处理结果 ClassStatic.tcpServer.Send(token, state, callbackId, ClassStatic.GetBytes("处理完毕")); } }
/// <summary> /// 客户端信息处理 /// </summary> /// <param name="obj">客户端信息</param> private void ClientReceive(object obj) { Client client = (Client)obj; while (client.socket.Connected) { try { byte[] temp = new byte[1024]; byte[] data = new byte[1024]; List <byte> byteSource = new List <byte>(); int length = client.socket.Receive(data, data.Length, 0); byteSource.AddRange(data.Take(length)); while (client.socket.Available > 0) { Thread.Sleep(100); int len = client.socket.Receive(temp, temp.Length, 0); if (len > 0) { byteSource.AddRange(temp.Take(len)); } } data = byteSource.ToArray(); length = data.Length; if (length > 3) { // 获取标识码 int state; if (System.Int32.TryParse(System.Text.Encoding.UTF8.GetString(data.Take(3).ToArray()), out state)) { string callbackId = ""; // 是否使用回调函数 uint isCallback = data[3]; // 截取数据 data = data.Skip(4).ToArray(); // 获取回调ID if (isCallback == 1) { callbackId = ClassStatic.GetString(data.Take(32).ToArray()); data = data.Skip(32).ToArray(); } // 100是心跳检测包 if (state == 100) { client.dateTime = DateTime.Now.AddMinutes(5); socketClientList[client.token] = client; Send(client.token, 100, callbackId, data); } else { // 执行处理函数 OnClientReceiveHandler?.Invoke(client.token, state, callbackId, data); } } // 丢弃错误标识码的包 } } catch (SocketException ex) { // 客户端断开连接 DisconnectClient(client.token); break; } catch { // 其他错误 } } // 循环结束 }
/// <summary> /// 修改密码发送手机验证码 /// </summary> public static void RetrievePasswordSendCode(string token, int state, string callbackId, byte[] data) { if (state == 106) { ClassStatic.Client client; DateTime dateTimeNow = DateTime.Now; string phone = ClassStatic.GetString(data); ClassStatic.Result resultData = new ClassStatic.Result("false"); if (ClassStatic.IsPhone(phone)) { // 客户端列表中不存在或者验证码过期才能发送 if (ClassStatic.clientList.ContainsKey(token)) { client = ClassStatic.clientList[token]; } else { client = new ClassStatic.Client(); client.login = false; client.state = 2; } // 判断验证码是否过期 if (client.effective == null || DateTime.Compare(client.effective, dateTimeNow) < 0) { // 设置手机号 client.phone = phone; System.Data.DataTable dataTable = ClassStatic.sqlServer.Field("*").Where("phone='" + phone + "'").Select("userInfo"); if (dataTable.Rows.Count > 0) { // 设置验证码 client.verifies = ClassStatic.GetVerifiesCode(); // 设置手机验证码过期时间 client.effective = DateTime.Now.AddMinutes(ClassStatic.verifiesEffectiveTime); // 通过API接口将验证码发送到对应手机号 resultData = ClassStatic.HttpGet(ClassStatic.urlApi + "index.php?type=2&code=" + client.verifies + "&time=" + ClassStatic.verifiesEffectiveTime + "&phone=" + phone); // 短信发送成功 if (resultData.ret == "true") { resultData.msg = "发送验证码成功!"; // 更新客户端信息 if (ClassStatic.clientList.ContainsKey(token)) { ClassStatic.clientList[token] = client; } else { ClassStatic.clientList.Add(token, client); } ClassStatic.formMain.ConsoleWrite("成功向手机号:" + phone + "发送了验证码:" + client.verifies); } } else { resultData.msg = "此手机号未注册"; } } else { resultData.msg = "发送短信超时,请稍后再尝试。"; } } else { resultData.msg = "请输入正确的手机号"; } // 向客户端返回处理结果 ClassStatic.tcpServer.Send(token, state, callbackId, ClassStatic.SetResultByte(resultData)); } }