using Container.Business; using Container.Common; using Container.Model; using Container.Win; using Fleck; using System; using System.Collections.Generic; using System.Configuration; using System.Diagnostics; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Windows; namespace Container.Services { public class WebSocketForKiosk { [DllImport("user32.dll", SetLastError = true)] static extern IntPtr FindWindow(string IpClassName, string IpWindowName); public static List allSocketsForKiosk = new List(); private Class_Log log = new Class_Log(); App app = ((App)Application.Current); PlayBackManage pbm = new PlayBackManage(); public void openSocket() { FleckLog.Level = LogLevel.Debug; var ws = ConfigurationManager.AppSettings["websocketForKiosk"]; var server = new WebSocketServer(ws); server.Start(socket => { socket.OnOpen = () => //当建立Socket链接时执行此方法 { //var data = socket.ConnectionInfo; //通过data可以获得这个链接传递过来的Cookie信息,用来区分各个链接和用户之间的关系(如果需要后台主动推送信息到某个客户的时候,可以使用Cookie) allSocketsForKiosk.Add(socket); }; socket.OnClose = () =>// 当关闭Socket链接十执行此方法 { //Console.WriteLine("Close!"); allSocketsForKiosk.Remove(socket); }; socket.OnMessage = message =>// 接收客户端发送过来的信息 { log.WriteLogFile(message, "kiosk"); if (message.Contains("version")) { app.kioskVersion = message.Substring(message.LastIndexOf(':') + 1); app.Appversion = app.kioskVersion; app.AppPlayNameCH = "导视"; app.AppPlayName = "chrome"; app.CurAppPlayName = app.AppPlayName; WebSocketForKiosk.allSocketsForKiosk.ToList().ForEach(s => s.Send("kiosk")); } else if (message == "startexplorer") { Process[] pro = Process.GetProcesses(); for (int i = 0; i < pro.Length; i++) { //if (pro[i].ProcessName.ToLower() == "chrome" ) if (pro[i].ProcessName.ToLower() == "chrome") { pro[i].CloseMainWindow(); } } System.Diagnostics.Process process = new System.Diagnostics.Process(); System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; startInfo.FileName = "cmd.exe"; process.StartInfo = startInfo; process.StartInfo.RedirectStandardInput = true; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.UseShellExecute = false; process.Start(); process.StandardInput.WriteLine(Environment.GetEnvironmentVariable("windir") + "\\explorer.exe"); process.StandardInput.Flush(); process.StandardInput.Close(); process.WaitForExit(); deleteSchtasks("ContainerService"); //Environment.Exit(0); } else if (message == "pcscreensavers") { bool pcstart = false; IntPtr Win = FindWindow(null, "WindowView");//设置置顶 if (Win != null && Win != IntPtr.Zero) { pcstart = true; //SetWindowPos(Win, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE); } if (!pcstart) { App.Current.Dispatcher.Invoke((Action)(() => { try { //app.IsCanConnect = MyHttpClient.IsCanConnect(app.HttpUrl); //if (app.IsCanConnect) //{ List result = pbm.GetResource(); //ScreenSavers result = pbm.GetResourceForLoad(); if ((result != null && result.Count > 0)) { app.AppPlayNameCH = "信息发布"; app.CurAppPlayName = "pcscreensavers"; app.AppPlayName = "pcscreensavers"; app.Appversion = app.Containerversion; WindowViewNew windowView = new WindowViewNew(0); windowView.Show(); } //} //else //{ // List> result = pbm.GetResource(1); // if (result != null && result.Count > 0) // { // app.AppPlayNameCH = "信息发布"; // app.CurAppPlayName = "pcscreensavers"; // app.AppPlayName = "pcscreensavers"; // app.Appversion = app.Containerversion; // WindowView windowView = new WindowView(0); // windowView.Show(); // } //} } catch (Exception ex) { log.WriteLogFile(ex.ToString(), "errorrrr"); } })); } } }; }); } private void deleteSchtasks(string schtaskName) { try { Process MyProcess = new Process(); //设定程序名 MyProcess.StartInfo.FileName = "cmd.exe"; //关闭Shell的使用 MyProcess.StartInfo.UseShellExecute = false; //重定向标准输入 MyProcess.StartInfo.RedirectStandardInput = true; //重定向标准输出 MyProcess.StartInfo.RedirectStandardOutput = true; //重定向错误输出 MyProcess.StartInfo.RedirectStandardError = true; //设置不显示窗口 MyProcess.StartInfo.CreateNoWindow = true; string cmdtext = "schtasks /delete /tn " + schtaskName + " -f"; //关机 log.WriteLogFile(cmdtext, "cmdexelog"); //执行VER命令 MyProcess.Start(); MyProcess.StandardInput.WriteLine(cmdtext); } catch (Exception ex) { log.WriteLogFile(ex.ToString()); } } } }