usingHFCentraControl.Common;usingHFCentraControl.Others;usingSuperSocket.WebSocket;usingSystem;usingSystem.Collections.Generic;usingSystem.Drawing;usingSystem.Linq;usingSystem.Net.WebSockets;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;usingWYL.Template.Common.Log;usingNewtonsoft;usingNewtonsoft.Json;usingSystem.Threading;usingSystem.Runtime.InteropServices;namespaceWindowsFormsApp2
{public classWebScoketEx
{staticWebSocketServer wsServer;public static voidStart()
{if (wsServer == null)
{
wsServer= newWebSocketServer();if (!wsServer.Setup("192.168.100.188", 6789))
{//设置IP 与 端口失败 通常是IP 和端口范围不对引起的 IPV4 IPV6
}
}if (!wsServer.Start())
{//开启服务失败 基本上是端口被占用或者被 某杀毒软件拦截造成的
return;
}
wsServer.NewSessionConnected+= (session) =>{//有新的连接
AppReportManager.Instance.Send(newLogEntity()
{
Log= "有新的连接...."});
};
wsServer.SessionClosed+= (session, reason) =>{//有断开的连接
AppReportManager.Instance.Send(newLogEntity()
{
Log= "有断开的连接...."});
};
wsServer.NewMessageReceived+= (session, message) =>{//接收到新的文本消息
AppReportManager.Instance.Send(newLogEntity()
{
Log= "接收到新的文本消息...."});var cmd = JsonConvert.DeserializeObject(message);if (cmd.action != 0x02)
{//lastActionTime = DateTime.Now;
}if (cmd.action!=4)
{
AppReportManager.Instance.Send(newLogEntity()
{
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(newLogEntity()
{
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 insessions)
{
item.Send(screen,0, screen.Length);
}
}
Thread.Sleep(10);
}catch(Exception ex)
{
AppReportManager.Instance.Send(newLogEntity()
{
Log= "Send出错,ex:" +ex.ToString()
});
}
}
});
}#region 截取屏幕图像
private staticBitmap 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);returntSrcBmp;
}#endregion[System.Runtime.InteropServices.DllImport("user32")]private static extern int mouse_event(int dwFlags, int dx, int dy, int dwData, intdwExtraInfo);//移动鼠标
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, inty);private static voidCmd(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);
}
}
}
}