You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

203 lines
5.8 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using NLog;
using POSV.ShoppingCart;
using WebSocket4Net;
using POSV.Bean;
namespace POSV.Utils
{
public class WSSE
{
private static Logger logger = NLog.LogManager.GetCurrentClassLogger();
private static object _lock = new object();
private System.Timers.Timer _reconnectTimer;
private static WSSE _instance = null;
private WebSocketEx _websocket = null;
private bool _connected = false;
public static WSSE Instance
{
get
{
if (_instance == null)
{
lock (_lock)
{
_instance = new WSSE();
_instance.InitWebSocket();
logger.Info("初始化服务......");
}
}
return _instance;
}
}
private void InitWebSocket()
{
try
{
//是否启用服务中心
var enable = Global.Instance.GlobalConfigBoolValue(ConfigConstant.DEVICE_SERVICECENTER_ENABLE, false);
var ip = Global.Instance.GlobalConfigStringValue(ConfigConstant.DEVICE_SERVICECENTER_IP);
var port = Global.Instance.GlobalConfigStringValue(ConfigConstant.DEVICE_SERVICECENTER_PORT);
var enableKds = Global.Instance.GlobalConfigBoolValue(ConfigConstant.DEVICE_KITCHEN_DISPLAY_ENABLE, false);
if (enable && !string.IsNullOrEmpty(ip) && !string.IsNullOrEmpty(port))
{
if (enableKds)
{
var url = string.Format("ws://{0}:{1}/{2}", ip, port, "pos-" + Global.Instance.Authc.PosNo);
//var url = string.Format("ws://{0}:{1}/{2}", ip, port, "websocket");
this._websocket = new WebSocketEx(url);
this._websocket.EnableAutoSendPing = true;
this._websocket.AutoSendPingInterval = 5;
this._websocket.Opened -= WebSocketOpened;
this._websocket.Opened += WebSocketOpened;
this._websocket.Error -= WebSocketError;
this._websocket.Error += WebSocketError;
this._websocket.Closed -= WebSocketClosed;
this._websocket.Closed += WebSocketClosed;
if (_reconnectTimer == null)
{
_reconnectTimer = new System.Timers.Timer(40000);
}
_reconnectTimer.AutoReset = true;
_reconnectTimer.Enabled = true;
_reconnectTimer.Elapsed -= ReconnectTimerCallback;
_reconnectTimer.Elapsed += ReconnectTimerCallback;
_reconnectTimer.Start();
}
else
{
logger.Error("未启用厨显不启用websocket服务");
}
}
else
{
logger.Error("未配置或未启用服务中心");
}
}
catch (Exception ex)
{
logger.Debug(ex, "InitWebSocket");
}
}
public void Query<T>(string name, object content, Action<T> executor)
{
if (this._websocket != null)
{
this._websocket.Query<T>(name, content, executor);
}
}
//public void On<T>(string name , Action<WebSocketEx , T> executor)
//{
// if(this._websocket != null)
// {
// }
//}
public void On<T>(string name, Action<T> executor)
{
logger.Info("收到消息" + name);
if (this._websocket != null)
{
this._websocket.On<T>(name, executor);
}
}
private void WebSocketClosed(object sender, EventArgs e)
{
this._connected = false;
}
private void WebSocketError(object sender, SuperSocket.ClientEngine.ErrorEventArgs e)
{
this._connected = false;
}
private void WebSocketOpened(object sender, EventArgs e)
{
this._connected = true;
}
/// <summary>
/// 重新连接服务中心
/// </summary>
public void RestartConnect()
{
//停止自动重连任务
if (_reconnectTimer != null)
{
_reconnectTimer.Close();
}
//关闭连接
if (this._websocket != null && this._websocket.State != WebSocketState.Closed)
{
this._websocket.Close();
}
//重新连接
StartConnect();
}
void StartConnect()
{
this._websocket = null;
InitWebSocket();
if (this._websocket != null)
{
this._websocket.Open();
}
}
private void ReconnectTimerCallback(object sender, System.Timers.ElapsedEventArgs e)
{
try
{
//already open or openning
if (this._websocket.State == WebSocketState.Connecting || this._websocket.State == WebSocketState.Open)
{
return;
}
logger.Info("尝试连接服务中心……");
StartConnect();
}
catch (Exception ex)
{
logger.Error(ex, "连接服务中心错误");
}
}
}
}