|
|
|
@ -8,6 +8,10 @@ using System.Net.WebSockets;
|
|
|
|
|
using System.Text; |
|
|
|
|
using System.Threading; |
|
|
|
|
using System.Threading.Tasks; |
|
|
|
|
using System.Net.Sockets; |
|
|
|
|
using System.Timers; |
|
|
|
|
using CPF.Mac.Foundation; |
|
|
|
|
using CPF.Windows; |
|
|
|
|
|
|
|
|
|
namespace AksWebBrowser.Common |
|
|
|
|
{ |
|
|
|
@ -16,6 +20,8 @@ namespace AksWebBrowser.Common
|
|
|
|
|
private readonly string _serverUri; |
|
|
|
|
private readonly ClientWebSocket _webSocket = new ClientWebSocket(); |
|
|
|
|
public string funName = string.Empty; |
|
|
|
|
//开启定时 |
|
|
|
|
System.Timers.Timer timer = new System.Timers.Timer(30000); |
|
|
|
|
public WebSocketClientWithHeartbeat() |
|
|
|
|
{ |
|
|
|
|
try |
|
|
|
@ -52,10 +58,11 @@ namespace AksWebBrowser.Common
|
|
|
|
|
// 处理接收到的消息逻辑 |
|
|
|
|
string body = System.Text.Encoding.UTF8.GetString(buffer, 0, result.Count); |
|
|
|
|
if (!string.IsNullOrEmpty(body)) |
|
|
|
|
{ |
|
|
|
|
if (funName == "CF_StartLiveDetect") |
|
|
|
|
{ |
|
|
|
|
JObject jo = (JObject)JsonConvert.DeserializeObject(body); |
|
|
|
|
string functionName = jo["functionName"].ToString(); |
|
|
|
|
if (functionName == "LiveDetectResult") |
|
|
|
|
{ |
|
|
|
|
string status = string.Empty; |
|
|
|
|
if (body.Contains("success")) |
|
|
|
|
{ |
|
|
|
@ -63,24 +70,58 @@ namespace AksWebBrowser.Common
|
|
|
|
|
} |
|
|
|
|
if (string.IsNullOrEmpty(status)) |
|
|
|
|
{ |
|
|
|
|
string functionName = jo["functionName"].ToString(); |
|
|
|
|
string nEventId = jo["nEventId"].ToString(); |
|
|
|
|
if (functionName == "LiveDetectResult" && nEventId == "100") |
|
|
|
|
if (nEventId == "100") |
|
|
|
|
{ |
|
|
|
|
Parame.messageData = body; |
|
|
|
|
} |
|
|
|
|
string results = Parame.ImgBase64; |
|
|
|
|
if (!string.IsNullOrEmpty(results)) |
|
|
|
|
{ |
|
|
|
|
JObject jo1 = (JObject)JsonConvert.DeserializeObject(results); |
|
|
|
|
string data = jo1["data"].ToString(); |
|
|
|
|
jo1 = (JObject)JsonConvert.DeserializeObject(data); |
|
|
|
|
string data2 = jo1["Data"].ToString(); |
|
|
|
|
jo1 = (JObject)JsonConvert.DeserializeObject(data2); |
|
|
|
|
string ImageBase64 = jo1["ImageBase64"].ToString(); |
|
|
|
|
|
|
|
|
|
var faceA = "/data/home/aks/aks/wwwroot/TmpFile/faceA.jpg"; |
|
|
|
|
var faceB = "/data/home/aks/aks/wwwroot/TmpFile/faceB.jpg"; |
|
|
|
|
//string faceA = Utils.Base64ByImagesPath(ImageBase64.Replace("data:image/jpg;base64,", ""), "faceA"); |
|
|
|
|
//string faceB = Utils.Base64ByImagesPath(jo["ImgBase64"].ToString(), "faceB"); |
|
|
|
|
var mode2 = new |
|
|
|
|
{ |
|
|
|
|
function = "CF_FaceCompare", |
|
|
|
|
filePathA = faceA, |
|
|
|
|
filePathB = faceB |
|
|
|
|
}; |
|
|
|
|
//开始人脸比对 |
|
|
|
|
Send(JsonConvert.SerializeObject(mode2), "CF_FaceCompare"); |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
if (status != "0") |
|
|
|
|
{ |
|
|
|
|
Parame.messageData = body; |
|
|
|
|
results = "{\"timestamp\":\"" + Utils.GetTimestamp() + "\",\"keycode\":\"" + "aks100101" + "\",\"message\":\"fali\",\"code\":\"400\",\"status\":false,\"data\":\"" + "人证合一验证失败." + "\"}"; |
|
|
|
|
Parame.socket.Send(results); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
else if (functionName == "CF_FaceCompare") |
|
|
|
|
{ |
|
|
|
|
//停止活检 |
|
|
|
|
Send("{\"function\":\"CF_StopLiveDetect\"}", "CF_StopLiveDetect"); |
|
|
|
|
//关闭摄像头 |
|
|
|
|
Send("{\"function\":\"CF_CloseCamera\"}", "CF_CloseCamera"); |
|
|
|
|
string status = jo["success"].ToString(); |
|
|
|
|
string results = string.Empty; |
|
|
|
|
if (status == "0") |
|
|
|
|
{ |
|
|
|
|
results = Parame.ImgBase64; |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
Parame.messageData = body; |
|
|
|
|
results = "{\"timestamp\":\"" + Utils.GetTimestamp() + "\",\"keycode\":\"" + "aks100101" + "\",\"message\":\"fali\",\"code\":\"400\",\"status\":false,\"data\":\"" + "人证合一验证失败." + "\"}"; |
|
|
|
|
} |
|
|
|
|
Parame.socket.Send(results); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -88,7 +129,6 @@ namespace AksWebBrowser.Common
|
|
|
|
|
catch (Exception ex) |
|
|
|
|
{ |
|
|
|
|
Log.Error("接收消息双目websockt" + ex.Message); |
|
|
|
|
Parame.messageData = "400"; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -121,7 +161,7 @@ namespace AksWebBrowser.Common
|
|
|
|
|
/// </summary> |
|
|
|
|
/// <param name="message"></param> |
|
|
|
|
/// <returns></returns> |
|
|
|
|
public async Task<string> Send(string message, string _funName) |
|
|
|
|
public async Task Send(string message, string _funName) |
|
|
|
|
{ |
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
@ -132,27 +172,36 @@ namespace AksWebBrowser.Common
|
|
|
|
|
// 发送消息到服务器 |
|
|
|
|
var buffer = System.Text.Encoding.UTF8.GetBytes(message); |
|
|
|
|
await _webSocket.SendAsync(new ArraySegment<byte>(buffer), WebSocketMessageType.Text, true, CancellationToken.None); |
|
|
|
|
int timeout = 0; |
|
|
|
|
while (string.IsNullOrEmpty(Parame.messageData) && timeout < 30000) |
|
|
|
|
{ |
|
|
|
|
timeout = timeout + 10; |
|
|
|
|
Task.Delay(10).Wait(); |
|
|
|
|
} |
|
|
|
|
if (timeout >= 30000) |
|
|
|
|
if (_funName== "CF_StartLiveDetect") |
|
|
|
|
{ |
|
|
|
|
Parame.messageData = "500"; |
|
|
|
|
timer.Elapsed += OnTimedEvent; |
|
|
|
|
timer.Enabled = true;//启动定时器 |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
catch (Exception ex) |
|
|
|
|
{ |
|
|
|
|
Parame.messageData = "700"; |
|
|
|
|
Log.Error("推送消息到双目websockt" + ex.Message); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
catch (Exception ex) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//打开串口 |
|
|
|
|
private void OnTimedEvent(Object source, ElapsedEventArgs e) |
|
|
|
|
{ |
|
|
|
|
Log.Error("推送消息到双目websockt" + ex.Message); |
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
timer.Stop(); |
|
|
|
|
//停止活检 |
|
|
|
|
Send("{\"function\":\"CF_StopLiveDetect\"}", "CF_StopLiveDetect"); |
|
|
|
|
//关闭摄像头 |
|
|
|
|
Send("{\"function\":\"CF_CloseCamera\"}", "CF_CloseCamera"); |
|
|
|
|
string results = "{\"timestamp\":\"" + Utils.GetTimestamp() + "\",\"keycode\":\"" + "aks100101" + "\",\"message\":\"fali\",\"code\":\"400\",\"status\":false,\"data\":\"" + "活体检测超时." + "\"}"; |
|
|
|
|
Parame.socket.Send(results); |
|
|
|
|
} |
|
|
|
|
return Parame.messageData; |
|
|
|
|
catch { } |
|
|
|
|
finally { } |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
|