void _server_MessageReceived(object sender, SocketLibrary.SocketBase.MessageEventArgs e) { System.Console.WriteLine("收到调试信息:" + e.Message.MessageBody); try { SpeakerLibrary.Message.DebugMessage dm = SpeakerLibrary.Message.DebugMessage.FromJson(e.Message.MessageBody); switch (dm.Command) { case CommandConst.ActionRun: //动作记录 ActionObject ao = ((Newtonsoft.Json.Linq.JToken)dm.Content).ToObject <ActionObject>(); //入队 TaskQueues.Enqueue(new DebugActionQueueObject(e.Connecction.ConnectionName, dm.MsgId, ao)); break; case CommandConst.UploadDataBase: //更新动作库 UpdateDB(dm); break; } //发送回复 SendMessage(e.Connecction, dm); } catch (Exception ex) { System.Console.WriteLine(ex.ToString()); } }
private void btnRunAction_Click(object sender, EventArgs e) { if (dgActions.SelectedRows.Count > 0 && dgActions.SelectedRows[0].Tag != null && Client != null) { if (MessageBox.Show("真的要执行吗?", "提示", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes) { try { //构造消息 SpeakerLibrary.Message.DebugMessage dm = new SpeakerLibrary.Message.DebugMessage(); dm.Command = CommandConst.ActionRun; dm.MsgId = Guid.NewGuid().ToString(); SpeakerLibrary.Message.ActionObject actionObject = new SpeakerLibrary.Message.ActionObject(); actionObject.Action = (Robot_Actions)dgActions.SelectedRows[0].Tag; actionObject.StepList = DBInstance.DbHelper.table("Robot_Steps").where ("ActionId=?", new object[] { actionObject.Action.Id }).select("*").getList <Robot_Steps>(new Robot_Steps()).ToArray(); dm.Content = actionObject; //发送消息 SocketLibrary.Connection connection; Client.Connections.TryGetValue(Client.ClientName, out connection); if (connection != null) { SocketLibrary.Message message = new SocketLibrary.Message(SocketLibrary.Message.CommandType.SendMessage, SpeakerLibrary.Message.DebugMessage.ToJson(dm)); connection.messageQueue.Enqueue(message); } MessageBox.Show("发送完成"); } catch (Exception ex) { MessageBox.Show("发送失败!"); } } } }
/// <summary> /// 转换到Json字符串 /// </summary> /// <param name="dm"></param> /// <returns></returns> public static string ToJson(DebugMessage dm) { return(JsonConvert.SerializeObject(dm)); }