|
|
|
|
using Fleck;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Kingo.PluginServiceInterface
|
|
|
|
|
{
|
|
|
|
|
public class WebsocketHelper
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
private WebSocketServer webSocketServer;
|
|
|
|
|
public IWebSocketConnection Conn
|
|
|
|
|
{
|
|
|
|
|
private get { return conn; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
conn = value;
|
|
|
|
|
if (!conns.ContainsKey(value.ConnectionInfo.Id.ToString()))
|
|
|
|
|
conns.Add(value.ConnectionInfo.Id.ToString(), value);
|
|
|
|
|
conn.OnClose = () => { };
|
|
|
|
|
conn.OnMessage = message =>
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(message.Trim()) == false)
|
|
|
|
|
{
|
|
|
|
|
//var result = CommandHelper.Exc(message);
|
|
|
|
|
//socket.Send(message + Environment.NewLine);
|
|
|
|
|
}
|
|
|
|
|
OnMessageCallback?.Invoke(message);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public Dictionary<string, IWebSocketConnection> conns = new Dictionary<string, IWebSocketConnection>();
|
|
|
|
|
public static WebsocketHelper Instance { get; } = new WebsocketHelper();
|
|
|
|
|
public Action<object> ConnectionCallback;
|
|
|
|
|
public Action<object> OnMessageCallback;
|
|
|
|
|
private IWebSocketConnection conn;
|
|
|
|
|
|
|
|
|
|
private WebsocketHelper()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="port"></param>
|
|
|
|
|
public void Start(int port)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (webSocketServer == null)
|
|
|
|
|
{
|
|
|
|
|
webSocketServer = new WebSocketServer(string.Format("ws://0.0.0.0:{0}", port));
|
|
|
|
|
}
|
|
|
|
|
webSocketServer.Start(ConnectionCallback);
|
|
|
|
|
//webSocketServer.Start(socket =>
|
|
|
|
|
//{
|
|
|
|
|
// Conn = socket;
|
|
|
|
|
// //socket.OnOpen = () => Conn.Send("服务器已连接!");
|
|
|
|
|
// socket.OnClose = () => { };
|
|
|
|
|
// socket.OnMessage = message =>
|
|
|
|
|
// {
|
|
|
|
|
// if (string.IsNullOrEmpty(message.Trim()) == false)
|
|
|
|
|
// {
|
|
|
|
|
// //var result = CommandHelper.Exc(message);
|
|
|
|
|
// //socket.Send(message + Environment.NewLine);
|
|
|
|
|
// }
|
|
|
|
|
// };
|
|
|
|
|
//});
|
|
|
|
|
}
|
|
|
|
|
catch { }
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
public void Send(string pMsg)
|
|
|
|
|
{
|
|
|
|
|
foreach (var item in conns.Keys)
|
|
|
|
|
{
|
|
|
|
|
if (conns[item] != null)
|
|
|
|
|
{
|
|
|
|
|
if (conns[item].IsAvailable)
|
|
|
|
|
{
|
|
|
|
|
conns[item].Send(pMsg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Stop()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
foreach (var item in conns.Keys)
|
|
|
|
|
{
|
|
|
|
|
if (conns[item] != null)
|
|
|
|
|
{
|
|
|
|
|
conns[item].Close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
conns.Clear();
|
|
|
|
|
//if (Conn != null)
|
|
|
|
|
// Conn.Close();
|
|
|
|
|
//Conn = null;
|
|
|
|
|
if (webSocketServer != null)
|
|
|
|
|
{
|
|
|
|
|
webSocketServer.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch { }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|