|
|
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: 会话空闲超时时间; 当此会话空闲时间超过此值,同时clearIdleSession被配置成true时,此会话将会被关闭; 默认值为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)
|
|
|
//{
|
|
|
|
|
|
//}
|
|
|
}
|
|
|
}
|