7 changed files with 402 additions and 71 deletions
@ -0,0 +1,177 @@ |
|||||||
|
using AKSWebBrowser.Commen; |
||||||
|
using Newtonsoft.Json.Linq; |
||||||
|
using Newtonsoft.Json; |
||||||
|
using System; |
||||||
|
using System.Collections.Generic; |
||||||
|
using System.Linq; |
||||||
|
using System.Net.WebSockets; |
||||||
|
using System.Text; |
||||||
|
using System.Threading; |
||||||
|
using System.Threading.Tasks; |
||||||
|
|
||||||
|
namespace AksWebBrowser.Common |
||||||
|
{ |
||||||
|
public class WebSocketClientWithHeartbeat |
||||||
|
{ |
||||||
|
private readonly string _serverUri; |
||||||
|
private readonly ClientWebSocket _webSocket = new ClientWebSocket(); |
||||||
|
public string funName = string.Empty; |
||||||
|
public WebSocketClientWithHeartbeat() |
||||||
|
{ |
||||||
|
try |
||||||
|
{ |
||||||
|
_serverUri = Parame.smWebsocket; |
||||||
|
_webSocket.ConnectAsync(new Uri(_serverUri), CancellationToken.None); |
||||||
|
Task.WhenAll(ReceiveMessages(), SendHeartbeats()); |
||||||
|
} |
||||||
|
catch (Exception ex) |
||||||
|
{ |
||||||
|
Log.Error("连接双目webSocket" + ex.Message); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// 接收消息 |
||||||
|
/// </summary> |
||||||
|
/// <returns></returns> |
||||||
|
private async Task ReceiveMessages() |
||||||
|
{ |
||||||
|
while (_webSocket.State == WebSocketState.Open) |
||||||
|
{ |
||||||
|
try |
||||||
|
{ |
||||||
|
var buffer = new byte[1024 * 1024 * 5]; |
||||||
|
var result = await _webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None); |
||||||
|
if (result.MessageType == WebSocketMessageType.Close) |
||||||
|
{ |
||||||
|
// 关闭处理逻辑 |
||||||
|
await _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None); |
||||||
|
} |
||||||
|
else |
||||||
|
{ |
||||||
|
// 处理接收到的消息逻辑 |
||||||
|
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 status = string.Empty; |
||||||
|
if (body.Contains("success")) |
||||||
|
{ |
||||||
|
status = jo["success"].ToString(); |
||||||
|
} |
||||||
|
if (string.IsNullOrEmpty(status)) |
||||||
|
{ |
||||||
|
string functionName = jo["functionName"].ToString(); |
||||||
|
string nEventId = jo["nEventId"].ToString(); |
||||||
|
if (functionName == "LiveDetectResult" && nEventId == "100") |
||||||
|
{ |
||||||
|
Parame.messageData = body; |
||||||
|
} |
||||||
|
} |
||||||
|
else |
||||||
|
{ |
||||||
|
if (status != "0") |
||||||
|
{ |
||||||
|
Parame.messageData = body; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
else |
||||||
|
{ |
||||||
|
Parame.messageData = body; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
catch (Exception ex) |
||||||
|
{ |
||||||
|
Log.Error("接收消息双目websockt" + ex.Message); |
||||||
|
Parame.messageData = "400"; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// 发送心跳 |
||||||
|
/// </summary> |
||||||
|
/// <returns></returns> |
||||||
|
private async Task SendHeartbeats() |
||||||
|
{ |
||||||
|
while (_webSocket.State == WebSocketState.Open) |
||||||
|
{ |
||||||
|
try |
||||||
|
{ |
||||||
|
// 心跳消息逻辑 |
||||||
|
var heartbeat = "{\"function\":\"Heartbeat\"}"; |
||||||
|
var sendBuffer = System.Text.Encoding.UTF8.GetBytes(heartbeat); |
||||||
|
await _webSocket.SendAsync(new ArraySegment<byte>(sendBuffer), WebSocketMessageType.Text, true, CancellationToken.None); |
||||||
|
await Task.Delay(TimeSpan.FromSeconds(6), CancellationToken.None); //根据需要设置心跳间隔 |
||||||
|
} |
||||||
|
catch (Exception ex) |
||||||
|
{ |
||||||
|
Log.Error("心跳消息逻辑双目websockt" + ex.Message); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// 推送消息到双目websockt |
||||||
|
/// </summary> |
||||||
|
/// <param name="message"></param> |
||||||
|
/// <returns></returns> |
||||||
|
public async Task<string> Send(string message, string _funName) |
||||||
|
{ |
||||||
|
try |
||||||
|
{ |
||||||
|
funName = _funName; |
||||||
|
Parame.messageData = string.Empty; |
||||||
|
if (_webSocket.State == WebSocketState.Open) |
||||||
|
{ |
||||||
|
// 发送消息到服务器 |
||||||
|
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) |
||||||
|
{ |
||||||
|
Parame.messageData = "500"; |
||||||
|
} |
||||||
|
} |
||||||
|
else |
||||||
|
{ |
||||||
|
Parame.messageData = "700"; |
||||||
|
} |
||||||
|
} |
||||||
|
catch (Exception ex) |
||||||
|
{ |
||||||
|
Log.Error("推送消息到双目websockt" + ex.Message); |
||||||
|
} |
||||||
|
return Parame.messageData; |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// 关闭双目websockt |
||||||
|
/// </summary> |
||||||
|
/// <returns></returns> |
||||||
|
public async Task Close() |
||||||
|
{ |
||||||
|
try |
||||||
|
{ |
||||||
|
if (_webSocket.State == WebSocketState.Open) |
||||||
|
{ |
||||||
|
await _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None); |
||||||
|
} |
||||||
|
} |
||||||
|
catch (Exception ex) |
||||||
|
{ |
||||||
|
Log.Error("关闭双目websockt" + ex.Message); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue