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.

185 lines
6.6 KiB
C#

9 months ago
using log4net;
using SuperSocket.SocketBase;
using SuperSocket.SocketBase.Config;
using System;
using SuperSocket.SocketEngine;
using POSV.WindowsService.Logging;
using POSV.Service;
namespace POSV.WindowsService
{
public class WSE
{
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
private static object _lock = new object();
private static WSE _instance = null;
/// <summary>
///
/// </summary>
private bool _inited = false;
private bool _started = false;
private WebSocketServerEx _wsse = null;
public static WSE Instance
{
get
{
if (_instance == null)
{
lock (_lock)
{
_instance = new WSE();
_instance.InitWebSocketServer();
logger.Info("初始化服务......");
}
}
return _instance;
}
}
private void InitWebSocketServer()
{
try
{
/*
name: ;
serverType: ;
serverTypeName: serverTypes serverTypes ;
ip: ip Any - IPv4 IPv6Any - IPv6
port: ;
listenBacklog: ;
mode: Socket, Tcp () Udp;
disabled: ;
startupOrder: , bootstrap ;
sendTimeOut: ;
sendingQueueSize: , 5;
maxConnectionNumber: ;
receiveBufferSize: ;
sendBufferSize: ;
syncSend: , : false;
logCommand: ;
logBasicSessionActivity: session;
clearIdleSession: true false, false;
clearIdleSessionInterval: , 120, ;
idleSessionTimeOut: ; clearIdleSessiontrue; 300;
security: Empty, Tls, Ssl3. Socket;
maxRequestLength: 1024;
textEncoding: ASCII;
defaultCulture: thread culture, .Net 4.5 'None' ;
disableSessionSnapshot: , false.
sessionSnapshotInterval: , 5, ;
keepAliveTime: keep alive, 600, ;
keepAliveInterval: Keep alive, keep alive 60, ;
*/
var wsPort = Global.Instance.GlobalConfigIntValue(ConfigConstant.SYSTEM_DEFAULT_PORT, 2018);
var serverConfig = new ServerConfig
{
Port = wsPort, //set the listening port
Ip = "Any" ,
Name = "POSVServer" ,
//Other configuration options
Mode = SocketMode.Tcp ,
MaxConnectionNumber = 100 ,
IdleSessionTimeOut = 600 ,
TextEncoding = "UTF-8" ,
SyncSend = true ,
SendBufferSize = 102400 ,
ReceiveBufferSize = 102400 ,
MaxRequestLength = 102400 ,
ClearIdleSession = true ,
ClearIdleSessionInterval = 120 ,
LogBasicSessionActivity = true ,
LogAllSocketException = true ,
LogCommand = true,
KeepAliveTime = 300 ,
SendingQueueSize = 50
};
serverConfig.SyncSend = true;
this._wsse = new WebSocketServerEx();
if (!this._wsse.Setup(serverConfig,null,null,new NLogFactory()))
{
logger.Error("参数初始化失败!");
this._inited = false;
}
else
{
logger.Debug("参数初始化成功!");
this._inited = true;
}
var log = this._wsse.LogFactory;
}
catch (Exception ex)
{
logger.Error(ex,"初始化服务异常");
}
}
public void SendAll()
{
if (this._started && this._wsse != null)
{
var sess = this._wsse.GetAllSessions();
foreach(var s in sess)
{
//s.SendJsonMessage();
}
}
}
public void Start()
{
if(this._inited && this._wsse != null)
{
if (!this._wsse.Start())
{
this._started = false;
}
else
{
this._started = true;
}
}
}
public void Stop()
{
if (this._started && this._wsse != null)
{
this._wsse.Stop();
}
}
//private void OnSessionClosed(WebSocketSessionEx session, CloseReason value)
//{
//}
//private void OnNewMessageReceived(WebSocketSessionEx session, string value)
//{
//}
//private void OnNewSessionConnected(WebSocketSessionEx session)
//{
//}
}
}