You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
267 lines
9.1 KiB
267 lines
9.1 KiB
using Fleck;
|
|
using IOTContainer.Common;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using WebSocket4Net;
|
|
|
|
namespace IOTContainer.Communication
|
|
{
|
|
public class WebSocketCom
|
|
{
|
|
private static WebSocketCom _socket;
|
|
public static WebSocketCom Socket
|
|
{
|
|
get
|
|
{
|
|
if (_socket == null)
|
|
{
|
|
_socket = new WebSocketCom();
|
|
}
|
|
return _socket;
|
|
}
|
|
}
|
|
private static List<IWebSocketConnection> _allSockets = new List<IWebSocketConnection>();
|
|
private WebSocketServer _server;
|
|
private WebSocket _listenedSocket;
|
|
public delegate void playIndexDelegate();
|
|
public static event playIndexDelegate playIndexEvent;
|
|
public static void removeDelegate()
|
|
{
|
|
|
|
if (playIndexEvent != null)
|
|
{
|
|
System.Delegate[] dels = playIndexEvent.GetInvocationList();
|
|
for (int i = 0; i < dels.Length; i++)
|
|
{
|
|
playIndexEvent -= dels[i] as playIndexDelegate;
|
|
}
|
|
}
|
|
}
|
|
#region 服务端
|
|
|
|
/// <summary>
|
|
/// 开启WebSocket服务
|
|
/// </summary>
|
|
public bool OpenSocketServer(string location)
|
|
{
|
|
try
|
|
{
|
|
_allSockets.Clear();
|
|
Log.MyLog.WriteLogFile($"开启WebSocket服务:{location}消息", "WebSocket");
|
|
_server = new WebSocketServer(location)
|
|
{
|
|
RestartAfterListenError = true
|
|
};
|
|
_server.Start(socket =>
|
|
{
|
|
socket.OnOpen = () =>
|
|
{
|
|
_allSockets.Add(socket);
|
|
Log.MyLog.WriteLogFile($"客户端连接:{socket.ConnectionInfo.ClientIpAddress}", "WebSocket");
|
|
};
|
|
socket.OnClose = () =>
|
|
{
|
|
_allSockets.Remove(socket);
|
|
Log.MyLog.WriteLogFile($"客户端连接断开:{socket.ConnectionInfo.ClientIpAddress}", "WebSocket");
|
|
};
|
|
socket.OnMessage = message =>//接受来自客户端的信息
|
|
{
|
|
};
|
|
});
|
|
ComParameters.Parameters.WebSocketIsConnected = true;
|
|
Log.MyLog.WriteLogFile($"开启WebSocket服务完成:{location}消息", "WebSocket");
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ComParameters.Parameters.WebSocketIsConnected = false;
|
|
Log.MyLog.WriteLogFile($"开启WebSocket服务出错:{location} " + ex.Message, "WebSocketError");
|
|
Task.Run(() =>
|
|
{
|
|
Retry(location);
|
|
|
|
});
|
|
return false;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 通知播放当前节目
|
|
/// </summary>
|
|
/// <param name="programmeIndex"></param>
|
|
public void ProgrammePlay(int programmeIndex)
|
|
{
|
|
try
|
|
{
|
|
Log.MyLog.WriteLogFile($"发送消息{programmeIndex}", "WebSocket");
|
|
foreach (var item in _allSockets)
|
|
{
|
|
|
|
item.Send(programmeIndex.ToString());
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.MyLog.WriteLogFile($"发送WebSocket消息出错:" + ex.Message, "WebSocketError");
|
|
}
|
|
|
|
//_allSockets.ForEach(s =>
|
|
//{
|
|
|
|
//});
|
|
}
|
|
/// <summary>
|
|
/// 停止websocketServer
|
|
/// </summary>
|
|
public void StopWebSocketServer()
|
|
{
|
|
_allSockets.ForEach((s) => s.Close());
|
|
_allSockets.Clear();
|
|
if (null != _server)
|
|
{
|
|
_server.Dispose();
|
|
}
|
|
}
|
|
public void Retry(string location)
|
|
{
|
|
Task.Delay(30000).Wait();
|
|
Log.MyLog.WriteLogFile($"尝试重新连接{location}", "WebSocketError");
|
|
if (!OpenSocketServer(location))
|
|
{
|
|
Retry(location);
|
|
}
|
|
}
|
|
#endregion
|
|
#region 客户端
|
|
private Timer timer;
|
|
public async void ListenWebSocket(string location)
|
|
{
|
|
try
|
|
{
|
|
Log.MyLog.WriteLogFile($"初始化监听Websocket", "WebSocket");
|
|
var token = new CancellationTokenSource();
|
|
_listenedSocket = new WebSocket(location);
|
|
_listenedSocket.Opened += _listenedServer_Opened;
|
|
_listenedSocket.Error += _listenedServer_Error;
|
|
_listenedSocket.Closed += _listenedServer_Closed;
|
|
_listenedSocket.MessageReceived += _listenedServer_MessageReceived;
|
|
|
|
_listenedSocket.Open();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.MyLog.WriteLogFile($"开启监听WebSocket服务出错:{location} " + ex.Message, "WebSocketError");
|
|
}
|
|
await Task.Run(() =>
|
|
{
|
|
RetryListen(location);
|
|
});
|
|
//timer = new Timer(_ => RetryListen(location), null, 0, 35000);
|
|
}
|
|
|
|
private void _listenedServer_MessageReceived(object sender, MessageReceivedEventArgs e)
|
|
{
|
|
try
|
|
{
|
|
//发送过来的消息
|
|
string message = e.Message;
|
|
Log.MyLog.WriteLogFile($"接收到Websocket消息:{message}", "WebSocket");
|
|
var success = int.TryParse(message, out int value);
|
|
if (success)
|
|
{
|
|
ComParameters.Parameters.ProgramIndex = value;
|
|
//if (null != ComParameters.Parameters.PlayToken)
|
|
//{
|
|
// ComParameters.Parameters.PlayToken.Cancel();
|
|
//}
|
|
playIndexEvent?.Invoke();
|
|
}
|
|
else
|
|
{
|
|
ComParameters.Parameters.ProgramIndex = null;
|
|
Log.MyLog.WriteLogFile("WebSocket接收的信息不是数字类型:", "WebSocketError");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
Log.MyLog.WriteLogFile("WebSocket接收的信息:" + ex.ToString(), "WebSocketError");
|
|
}
|
|
|
|
|
|
}
|
|
|
|
private void _listenedServer_Closed(object sender, EventArgs e)
|
|
{
|
|
ComParameters.Parameters.ProgramIndex = null;
|
|
Log.MyLog.WriteLogFile($"监听Websocket关闭", "WebSocket");
|
|
ListenWebSocket($"ws://{ComParameters.Parameters.MainMachineIp}:{ComParameters.Parameters.SocketPort}");
|
|
//Task.Run(() =>
|
|
//{
|
|
// RetryListen($"ws://{ComParameters.Parameters.MainMachineIp}:{ComParameters.Parameters.SocketPort}");
|
|
//});
|
|
}
|
|
|
|
private void _listenedServer_Error(object sender, SuperSocket.ClientEngine.ErrorEventArgs e)
|
|
{
|
|
ComParameters.Parameters.ProgramIndex = null;
|
|
Log.MyLog.WriteLogFile($"监听Websocket出错:{e.Exception.ToString()}", "WebSocketError");
|
|
}
|
|
|
|
private void _listenedServer_Opened(object sender, EventArgs e)
|
|
{
|
|
Log.MyLog.WriteLogFile($"初始化监听Websocket成功", "WebSocket");
|
|
}
|
|
|
|
public void SendMsg(string msg)
|
|
{
|
|
Task.Run(() =>
|
|
{
|
|
if (_listenedSocket != null && _listenedSocket.State == WebSocketState.Open)
|
|
{
|
|
_listenedSocket.Send(msg);
|
|
}
|
|
});
|
|
}
|
|
public async void StopListen()
|
|
{
|
|
try
|
|
{
|
|
ComParameters.Parameters.ProgramIndex = null;
|
|
if (_listenedSocket != null)
|
|
{
|
|
_listenedSocket.Close();
|
|
_listenedSocket.Dispose();
|
|
_listenedSocket = null;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.MyLog.WriteLogFile($"结束监听Websocket出错:{ex.Message}", "WebSocket");
|
|
}
|
|
}
|
|
public void RetryListen(string location)
|
|
{
|
|
try
|
|
{
|
|
if (_listenedSocket.State != WebSocketState.Open && _listenedSocket.State != WebSocketState.Connecting)
|
|
{
|
|
Log.MyLog.WriteLogFile($"尝试重新连接{location}", "WebSocket");
|
|
ComParameters.Parameters.ProgramIndex = null;
|
|
_listenedSocket.Close();
|
|
_listenedSocket.Open();
|
|
Task.Delay(5000).Wait();
|
|
RetryListen(location);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ComParameters.Parameters.ProgramIndex = null;
|
|
Log.MyLog.WriteLogFile($"尝试重新连接出错:{ex.Message}", "WebSocketError");
|
|
}
|
|
|
|
}
|
|
#endregion
|
|
}
|
|
}
|
|
|