|
@@ -6,41 +6,44 @@ using System;
|
|
|
using System.Collections.Concurrent;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
|
+
|
|
|
using Fleck;
|
|
|
+
|
|
|
using Sers.Core.CL.MessageDelivery;
|
|
|
+
|
|
|
using Vit.Core.Module.Log;
|
|
|
|
|
|
namespace Sers.CL.WebSocket
|
|
|
{
|
|
|
- public class DeliveryServer: IDeliveryServer
|
|
|
+ public class DeliveryServer : IDeliveryServer
|
|
|
{
|
|
|
public Sers.Core.Util.StreamSecurity.SecurityManager securityManager;
|
|
|
|
|
|
/// <summary>
|
|
|
/// 服务端地址(默认为 "ws://0.0.0.0:4503")
|
|
|
/// </summary>
|
|
|
- public string host="ws://0.0.0.0:4503";
|
|
|
+ public string host = "ws://0.0.0.0:4503";
|
|
|
|
|
|
|
|
|
public Action<IDeliveryConnection> Conn_OnDisconnected { private get; set; }
|
|
|
public Action<IDeliveryConnection> Conn_OnConnected { private get; set; }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
public bool Start()
|
|
|
{
|
|
|
Stop();
|
|
|
|
|
|
try
|
|
|
{
|
|
|
- Logger.Info("[CL.DeliveryServer] WebSocket,starting... host:" + host);
|
|
|
+ Logger.Info("[CL.DeliveryServer] WebSocket,starting", new { host });
|
|
|
|
|
|
connMap.Clear();
|
|
|
|
|
|
- listenSocket = new WebSocketServer(host);
|
|
|
+ listenSocket = new WebSocketServer(host);
|
|
|
|
|
|
//出错后进行重启
|
|
|
- listenSocket.RestartAfterListenError = true;
|
|
|
+ listenSocket.RestartAfterListenError = true;
|
|
|
|
|
|
#region Start
|
|
|
//开始监听
|
|
@@ -53,20 +56,20 @@ namespace Sers.CL.WebSocket
|
|
|
conn.securityManager = securityManager;
|
|
|
conn.Init(socket);
|
|
|
|
|
|
- socket.OnError = (ex) =>
|
|
|
+ socket.OnError = (ex) =>
|
|
|
{
|
|
|
Logger.Error(ex);
|
|
|
conn.Close();
|
|
|
};
|
|
|
- socket.OnOpen = () =>
|
|
|
+ socket.OnOpen = () =>
|
|
|
{
|
|
|
Delivery_OnConnected(conn);
|
|
|
};
|
|
|
- socket.OnClose = () =>
|
|
|
+ socket.OnClose = () =>
|
|
|
{
|
|
|
- Delivery_OnDisconnected(conn);
|
|
|
+ Delivery_OnDisconnected(conn);
|
|
|
};
|
|
|
- socket.OnBinary = bytes =>
|
|
|
+ socket.OnBinary = bytes =>
|
|
|
{
|
|
|
conn.AppendData(new ArraySegment<byte>(bytes, 0, bytes.Length));
|
|
|
};
|
|
@@ -76,7 +79,7 @@ namespace Sers.CL.WebSocket
|
|
|
});
|
|
|
#endregion
|
|
|
|
|
|
- Logger.Info("[CL.DeliveryServer] WebSocket,started.");
|
|
|
+ Logger.Info("[CL.DeliveryServer] WebSocket,started");
|
|
|
return true;
|
|
|
}
|
|
|
catch (Exception ex)
|
|
@@ -86,7 +89,7 @@ namespace Sers.CL.WebSocket
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// 停止服务
|
|
|
/// </summary>
|
|
@@ -98,55 +101,55 @@ namespace Sers.CL.WebSocket
|
|
|
listenSocket = null;
|
|
|
|
|
|
//(x.1) stop conn
|
|
|
- ConnectedList.ToList().ForEach(Delivery_OnDisconnected);
|
|
|
- connMap.Clear();
|
|
|
+ ConnectedList.ToList().ForEach(Delivery_OnDisconnected);
|
|
|
+ connMap.Clear();
|
|
|
|
|
|
//(x.2) close Socket
|
|
|
try
|
|
|
- {
|
|
|
- listenSocket_.Dispose();
|
|
|
+ {
|
|
|
+ listenSocket_.Dispose();
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
Logger.Error(ex);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
|
|
|
public DeliveryServer()
|
|
|
- {
|
|
|
+ {
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
|
|
|
WebSocketServer listenSocket;
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
|
|
|
/// <summary>
|
|
|
/// connHashCode -> DeliveryConnection
|
|
|
/// </summary>
|
|
|
readonly ConcurrentDictionary<int, DeliveryServer_Connection> connMap = new ConcurrentDictionary<int, DeliveryServer_Connection>();
|
|
|
|
|
|
-
|
|
|
|
|
|
|
|
|
- public IEnumerable<IDeliveryConnection> ConnectedList => connMap.Values.Select(conn=>((IDeliveryConnection)conn));
|
|
|
|
|
|
-
|
|
|
+ public IEnumerable<IDeliveryConnection> ConnectedList => connMap.Values.Select(conn => ((IDeliveryConnection)conn));
|
|
|
+
|
|
|
+
|
|
|
|
|
|
#region Delivery_Event
|
|
|
|
|
|
private DeliveryServer_Connection Delivery_OnConnected(DeliveryServer_Connection conn)
|
|
|
- {
|
|
|
-
|
|
|
+ {
|
|
|
+
|
|
|
conn.Conn_OnDisconnected = Delivery_OnDisconnected;
|
|
|
|
|
|
connMap[conn.GetHashCode()] = conn;
|
|
@@ -162,10 +165,10 @@ namespace Sers.CL.WebSocket
|
|
|
}
|
|
|
|
|
|
private void Delivery_OnDisconnected(IDeliveryConnection _conn)
|
|
|
- {
|
|
|
- var conn = (DeliveryServer_Connection)_conn;
|
|
|
+ {
|
|
|
+ var conn = (DeliveryServer_Connection)_conn;
|
|
|
|
|
|
- connMap.TryRemove(conn.GetHashCode(),out _);
|
|
|
+ connMap.TryRemove(conn.GetHashCode(), out _);
|
|
|
|
|
|
try
|
|
|
{
|