You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
269 lines
10 KiB
269 lines
10 KiB
|
|
|
|
using DemoUI.SDK;
|
|
using Emgu.CV;
|
|
using Fleck;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using System.Configuration;
|
|
using System.Runtime.InteropServices;
|
|
|
|
namespace DemoUI.Common
|
|
{
|
|
|
|
public class ChatWebSocketMiddleware
|
|
{
|
|
public static List<IWebSocketConnection> allSockets = new List<IWebSocketConnection>();
|
|
//public static List<IWebSocketConnection> allSocketsForTest = new List<IWebSocketConnection>();
|
|
public static List<IWebSocketConnection> allSocketsForLive = new List<IWebSocketConnection>();
|
|
//public static List<IWebSocketConnection> faceSockets = new List<IWebSocketConnection>();
|
|
public static Dictionary<string, IWebSocketConnection> faceSocketLists = new Dictionary<string, IWebSocketConnection>();
|
|
public Dictionary<string, IWebSocketConnection> faceSocketsList = new Dictionary<string, IWebSocketConnection>();
|
|
public static string CurFaceID = string.Empty;
|
|
public static Mat CurFaceImage = null;
|
|
public static cw_face_res_t? CurFaceModel = null;
|
|
public static object NoFaceModel = new { faceID = "", isValid = false, beautyScore = 0, faceImage = "", genderMale = "" };
|
|
public static bool isLive = false;
|
|
//public static bool isTestLive = false;
|
|
private QMLog log = new QMLog();
|
|
public void openSocket()
|
|
{
|
|
FleckLog.Level = LogLevel.Debug;
|
|
var ws = ConfigurationManager.AppSettings["websocketUrl"];
|
|
var server = new WebSocketServer(ws);
|
|
server.Start(socket =>
|
|
{
|
|
socket.OnOpen = () => //当建立Socket链接时执行此方法
|
|
{
|
|
allSockets.Clear();
|
|
//var data = socket.ConnectionInfo; //通过data可以获得这个链接传递过来的Cookie信息,用来区分各个链接和用户之间的关系(如果需要后台主动推送信息到某个客户的时候,可以使用Cookie)
|
|
allSockets.Add(socket);
|
|
if (!string.IsNullOrEmpty(CurFaceID)) {
|
|
socket.Send(CurFaceID);
|
|
}
|
|
};
|
|
|
|
socket.OnClose = () =>// 当关闭Socket链接十执行此方法
|
|
{
|
|
//Console.WriteLine("Close!");
|
|
allSockets.Remove(socket);
|
|
};
|
|
|
|
socket.OnMessage = message =>// 接收客户端发送过来的信息
|
|
{
|
|
if (message == "face")
|
|
{
|
|
if (!string.IsNullOrEmpty(CurFaceID))
|
|
{
|
|
Dictionary<string, Object> dicData = JsonConvert.DeserializeObject<Dictionary<string, Object>>(CurFaceID);
|
|
var faceData = new { faceID = dicData["faceID"], isValid = true, beautyScore = dicData["beautyScore"], faceImage = dicData["faceImage"], genderMale = dicData["genderMale"] };
|
|
socket.Send(JsonConvert.SerializeObject(faceData));
|
|
}
|
|
|
|
//if (CurFaceImage != null && CurFaceModel.HasValue)
|
|
// SendFaceForKiosk(CurFaceImage, CurFaceModel.Value, socket);
|
|
}
|
|
else if (message == "openlive")
|
|
{
|
|
isLive = true;
|
|
}
|
|
else if (message == "closelive")
|
|
{
|
|
log.WriteLogFile("进来了", "2222222222222");
|
|
ChatWebSocketMiddleware.isLive = false;
|
|
}
|
|
else if (message == "startexplorer")
|
|
{
|
|
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();
|
|
}
|
|
|
|
//Console.WriteLine(message);
|
|
//socket.Send("Echo: " + message);
|
|
//allSockets.ToList().ForEach(s => s.Send("Echo: " + message));
|
|
};
|
|
});
|
|
|
|
}
|
|
|
|
//public void openSocketForTest()
|
|
//{
|
|
// FleckLog.Level = LogLevel.Debug;
|
|
// var ws = "ws://192.168.1.128:7189";
|
|
// var server = new WebSocketServer(ws);
|
|
// server.Start(socket =>
|
|
// {
|
|
// socket.OnOpen = () => //当建立Socket链接时执行此方法
|
|
// {
|
|
// //var data = socket.ConnectionInfo; //通过data可以获得这个链接传递过来的Cookie信息,用来区分各个链接和用户之间的关系(如果需要后台主动推送信息到某个客户的时候,可以使用Cookie)
|
|
// allSocketsForTest.Add(socket);
|
|
// };
|
|
|
|
// socket.OnClose = () =>// 当关闭Socket链接十执行此方法
|
|
// {
|
|
// //Console.WriteLine("Close!");
|
|
// allSocketsForTest.Remove(socket);
|
|
// };
|
|
|
|
// socket.OnMessage = message =>// 接收客户端发送过来的信息
|
|
// {
|
|
// if (message == "openlive")
|
|
// {
|
|
// isTestLive = true;
|
|
// }
|
|
// else if (message == "closelive")
|
|
// {
|
|
// ChatWebSocketMiddleware.isTestLive = false;
|
|
// }
|
|
|
|
// //Console.WriteLine(message);
|
|
// //socket.Send("Echo: " + message);
|
|
// //allSockets.ToList().ForEach(s => s.Send("Echo: " + message));
|
|
// };
|
|
// });
|
|
|
|
//}
|
|
|
|
public void openSocketForLive()
|
|
{
|
|
FleckLog.Level = LogLevel.Debug;
|
|
var ws = ConfigurationManager.AppSettings["websocketForLive"];
|
|
var server = new WebSocketServer(ws);
|
|
server.Start(socket =>
|
|
{
|
|
socket.OnOpen = () => //当建立Socket链接时执行此方法
|
|
{
|
|
//ChatWebSocketMiddleware.isLive = true;
|
|
//var data = socket.ConnectionInfo.Id; //通过data可以获得这个链接传递过来的Cookie信息,用来区分各个链接和用户之间的关系(如果需要后台主动推送信息到某个客户的时候,可以使用Cookie)
|
|
allSocketsForLive.Clear();
|
|
|
|
allSocketsForLive.Add(socket);
|
|
};
|
|
|
|
socket.OnClose = () =>// 当关闭Socket链接十执行此方法
|
|
{
|
|
ChatWebSocketMiddleware.isLive = false;
|
|
//Console.WriteLine("Close!");
|
|
allSocketsForLive.Remove(socket);
|
|
};
|
|
|
|
socket.OnMessage = message =>// 接收客户端发送过来的信息
|
|
{
|
|
if (message == "openlive")
|
|
{
|
|
ChatWebSocketMiddleware.isLive = true;
|
|
}
|
|
else if (message == "closelive")
|
|
{
|
|
ChatWebSocketMiddleware.isLive = false;
|
|
}
|
|
//Console.WriteLine(message);
|
|
//socket.Send("Echo: " + message);
|
|
//allSockets.ToList().ForEach(s => s.Send("Echo: " + message));
|
|
};
|
|
});
|
|
|
|
}
|
|
|
|
|
|
public void openSocketForFace()
|
|
{
|
|
FleckLog.Level = LogLevel.Debug;
|
|
var ws = ConfigurationManager.AppSettings["websocketForFace"];
|
|
var server = new WebSocketServer(ws);
|
|
server.Start(socket =>
|
|
{
|
|
socket.OnOpen = () => //当建立Socket链接时执行此方法
|
|
{
|
|
log.WriteLogFile("屏保socket111111","face");
|
|
var data = socket.ConnectionInfo.ClientPort.ToString(); //通过data可以获得这个链接传递过来的Cookie信息,用来区分各个链接和用户之间的关系(如果需要后台主动推送信息到某个客户的时候,可以使用Cookie)
|
|
log.WriteLogFile(JsonConvert.SerializeObject(socket.ConnectionInfo), "123");
|
|
//if (!faceSocketsList.ContainsKey(data)) {
|
|
// faceSockets.Add(socket);
|
|
//}
|
|
//faceSockets.Clear();
|
|
if (socket.ConnectionInfo.Path.Length > 1)
|
|
{
|
|
if (faceSocketLists.ContainsKey("信发"))
|
|
{
|
|
faceSocketLists["信发"].Close();
|
|
faceSocketLists["信发"] = socket;
|
|
}
|
|
else
|
|
{
|
|
faceSocketLists.Add("信发", socket);
|
|
}
|
|
socket.Send("ok");
|
|
}
|
|
else {
|
|
if (faceSocketLists.ContainsKey("导视"))
|
|
{
|
|
faceSocketLists["导视"] = socket;
|
|
}
|
|
else
|
|
{
|
|
faceSocketLists.Add("导视", socket);
|
|
}
|
|
}
|
|
//if (!faceSockets.Contains(socket)) {
|
|
// faceSockets.Add(socket);
|
|
// }
|
|
// log.WriteLogFile(faceSockets.Count.ToString(), "123");
|
|
//if (!faceSockets.Contains(socket))
|
|
//{
|
|
// faceSockets.Add(socket);
|
|
//}
|
|
};
|
|
|
|
socket.OnMessage = message =>// 接收客户端发送过来的信息
|
|
{
|
|
log.WriteLogFile(message, "face");
|
|
|
|
if (message.IndexOf("导航") >= 0)
|
|
{
|
|
var faceData = new
|
|
{
|
|
faceID = "-1",
|
|
faceImage = message.Replace("导航", "")
|
|
};
|
|
if (faceSocketLists.ContainsKey("导视")) {
|
|
faceSocketLists["导视"].Send(JsonConvert.SerializeObject(faceData));
|
|
}
|
|
|
|
}
|
|
else if (message.IndexOf("首页") >= 0)
|
|
{
|
|
var faceData = new
|
|
{
|
|
faceID = "-1",
|
|
faceImage = message
|
|
};
|
|
if (faceSocketLists.ContainsKey("导视"))
|
|
{
|
|
faceSocketLists["导视"].Send(JsonConvert.SerializeObject(faceData));
|
|
}
|
|
}
|
|
//log.WriteLogFile(message,"人脸点击");
|
|
//allSockets.ForEach(s => s.Send("Echo: " + message));
|
|
//Console.WriteLine(message);
|
|
//socket.Send("Echo: " + message);
|
|
//allSockets.ToList().ForEach(s => s.Send("Echo: " + message));
|
|
};
|
|
});
|
|
|
|
}
|
|
}
|
|
}
|
|
|