using HFCentraControl.Common; using HFCentraControl.Others; using SuperSocket.WebSocket; using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Net.WebSockets; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using WYL.Template.Common.Log; using Newtonsoft; using Newtonsoft.Json; using System.Threading; using System.Runtime.InteropServices; namespace WindowsFormsApp2 { public class WebScoketEx { static WebSocketServer wsServer; public static void Start() { if (wsServer == null) { wsServer = new WebSocketServer(); if (!wsServer.Setup("192.168.100.188", 6789)) { //设置IP 与 端口失败 通常是IP 和端口范围不对引起的 IPV4 IPV6 } } if (!wsServer.Start()) { //开启服务失败 基本上是端口被占用或者被 某杀毒软件拦截造成的 return; } wsServer.NewSessionConnected += (session) => { //有新的连接 AppReportManager.Instance.Send(new LogEntity() { Log = "有新的连接...." }); }; wsServer.SessionClosed += (session, reason) => { //有断开的连接 AppReportManager.Instance.Send(new LogEntity() { Log = "有断开的连接...." }); }; wsServer.NewMessageReceived += (session, message) => { //接收到新的文本消息 AppReportManager.Instance.Send(new LogEntity() { Log = "接收到新的文本消息...." }); var cmd = JsonConvert.DeserializeObject<RemoteDesktopModel>(message); if (cmd.action != 0x02) { //lastActionTime = DateTime.Now; } if (cmd.action!=4) { AppReportManager.Instance.Send(new LogEntity() { Log = string.Format(">> action:{0},x:{1},y:{2},c:{3},k:{4}", cmd.action, cmd.x, cmd.y, cmd.c, cmd.k) }); } if (cmd.action == 0x03) { if (cmd.k == 201 || cmd.k == 200) { //SwitchToLanguageMode(); } else { SendKeys.SendWait(cmd.c.ToString()); //log.AppendFormat(">> SendKeys:({0})", cmd.c).AppendLine(); } } else { Cmd(cmd); } }; wsServer.NewDataReceived += (session, bytes) => { //接收到新的二进制消息 AppReportManager.Instance.Send(new LogEntity() { Log = "接收到新的二进制消息...." }); }; Task.Run(() => { while (true) { if (Configs.isBreak) { wsServer.Stop(); break; } try { var screen = CommonHelper.Bitmap2Byte(GetScreenCapture()); var sessions = wsServer.GetAllSessions(); if (sessions!=null) { foreach (var item in sessions) { item.Send(screen, 0, screen.Length); } } Thread.Sleep(10); } catch (Exception ex) { AppReportManager.Instance.Send(new LogEntity() { Log = "Send出错,ex:" + ex.ToString() }); } } }); } #region 截取屏幕图像 private static Bitmap GetScreenCapture() { Rectangle tScreenRect = new Rectangle(0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); Bitmap tSrcBmp = new Bitmap(tScreenRect.Width, tScreenRect.Height); // 用于屏幕原始图片保存 Graphics gp = Graphics.FromImage(tSrcBmp); gp.CopyFromScreen(0, 0, 0, 0, tScreenRect.Size); gp.DrawImage(tSrcBmp, 0, 0, tScreenRect, GraphicsUnit.Pixel); return tSrcBmp; } #endregion [System.Runtime.InteropServices.DllImport("user32")] private static extern int mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo); //移动鼠标 const int MOUSEEVENTF_MOVE = 0x0001; //模拟鼠标左键按下 const int MOUSEEVENTF_LEFTDOWN = 0x0002; //模拟鼠标左键抬起 const int MOUSEEVENTF_LEFTUP = 0x0004; //模拟鼠标右键按下 const int MOUSEEVENTF_RIGHTDOWN = 0x0008; //模拟鼠标右键抬起 const int MOUSEEVENTF_RIGHTUP = 0x0010; //模拟鼠标中键按下 const int MOUSEEVENTF_MIDDLEDOWN = 0x0020; //模拟鼠标中键抬起 const int MOUSEEVENTF_MIDDLEUP = 0x0040; //标示是否采用绝对坐标 const int MOUSEEVENTF_ABSOLUTE = 0x8000; //模拟鼠标滚轮滚动操作,必须配合dwData参数 const int MOUSEEVENTF_WHEEL = 0x0800; [DllImport("user32.dll")] private static extern int SetCursorPos(int x, int y); private static void Cmd(RemoteDesktopModel cmd) { if (cmd.action == 1) { //mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); } else if (cmd.action == 2) { //mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0); //mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); //mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0); //mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); } else if (cmd.action == 3) { //SendKeys.SendWait(cmd.k.ToString()); } else if (cmd.action == 4) { SetCursorPos( cmd.x, cmd.y);//相对当前鼠标位置x轴和y轴分别移动50像素 } else if (cmd.action == 5) { mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0); } else if (cmd.action == 6) { mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); } else if (cmd.action == 7) { //mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0); //mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0); mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0); } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace HFCentraControl.Others { public struct RemoteDesktopModel { /// <summary> /// 1=单击,2=双击 ,3=键盘按键, 4=移动, 5=左键按下,6左键抬起,7=鼠标右击 /// </summary> public int action { get; set; } /// <summary> /// 功能键 /// </summary> public int c { get; set; } /// <summary> /// x坐标 /// </summary> public int x { get; set; } /// <summary> /// y坐标 /// </summary> public int y { get; set; } /// <summary> /// 按键 /// </summary> public int k { get; set; } } }