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.
349 lines
15 KiB
349 lines
15 KiB
using Newtonsoft.Json;
|
|
using PCScreenSavers.ViewModel;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Net;
|
|
using System.Threading;
|
|
using System.Web;
|
|
using WebSocket4Net;
|
|
|
|
namespace PCScreenSavers.Common
|
|
{
|
|
public class WebSocketManage
|
|
{
|
|
private WebSocket websocket = null;
|
|
App app = ((App)System.Windows.Application.Current);
|
|
public delegate void WebSocketReceiveDelegate(string type, string message);
|
|
public static event WebSocketReceiveDelegate WebSocketReceiveEvent;
|
|
private Class_Log log = new Class_Log();
|
|
bool reconnect = false;
|
|
#region WebSocketClient
|
|
|
|
public void OpenWebSocket()
|
|
{
|
|
try
|
|
{
|
|
if (string.IsNullOrEmpty(app.LocalIP))
|
|
{
|
|
foreach (IPAddress _IPAddress in Dns.GetHostEntry(Dns.GetHostName()).AddressList)
|
|
{
|
|
if (_IPAddress.AddressFamily.ToString() == "InterNetwork")
|
|
{
|
|
app.LocalIP = _IPAddress.ToString();
|
|
}
|
|
}
|
|
}
|
|
string sendmsg = "ip=" + app.LocalIP+"msg";
|
|
websocket = new WebSocket(app.configwebsocket + "?" + sendmsg);// 地址
|
|
websocket.Error += new EventHandler<SuperSocket.ClientEngine.ErrorEventArgs>(websocket_Error);
|
|
websocket.MessageReceived += new EventHandler<MessageReceivedEventArgs>(websocket_MessageReceived);
|
|
websocket.Open();
|
|
//websocket.Opened += new EventHandler(websocket_Opened);
|
|
websocket.Closed += new EventHandler(websocket_Closed);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
log.WriteLogFile(ex.ToString());
|
|
}
|
|
}
|
|
|
|
void websocket_Closed(object sender, EventArgs e)
|
|
{
|
|
if (!reconnect)
|
|
{
|
|
reconnect = true;
|
|
ReConnect();
|
|
}
|
|
}
|
|
|
|
public void CloseWebSocket()
|
|
{
|
|
//if (websocket != null)
|
|
// websocket.Close();
|
|
}
|
|
|
|
void websocket_MessageReceived(object sender, MessageReceivedEventArgs e)
|
|
{
|
|
try
|
|
{
|
|
log.WriteLogFile(e.Message, "WebSocketLog");
|
|
|
|
string type = "";
|
|
Dictionary<string, Object> dic = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, Object>>(e.Message);
|
|
if (dic["Content"] != null && dic["SenderID"] != null)
|
|
{
|
|
Dictionary<string, Object> Commands = JsonConvert.DeserializeObject<Dictionary<string, Object>>(dic["Content"].ToString());
|
|
|
|
if (!string.IsNullOrEmpty(Commands["Type"].ToString()))
|
|
{
|
|
type = Commands["Type"].ToString();
|
|
}
|
|
if (type.Trim().Equals("relay"))
|
|
{
|
|
log.WriteLogFile("接地导航", "WebSocketLog");
|
|
if (!string.IsNullOrEmpty(Commands["Parameter"].ToString()))
|
|
{
|
|
log.WriteLogFile(Commands["Parameter"].ToString(), "WebSocketLog");
|
|
Dictionary<string, Object> data = JsonConvert.DeserializeObject<Dictionary<string, Object>>(Commands["Parameter"].ToString());
|
|
if (!string.IsNullOrEmpty(data["FloorName"].ToString()))
|
|
{
|
|
app.navModel.FloorName = data["FloorName"].ToString();
|
|
}
|
|
else {
|
|
app.navModel.FloorName = "";
|
|
}
|
|
if (!string.IsNullOrEmpty(data["Angle"].ToString()))
|
|
{
|
|
app.navModel.Angle = Convert.ToInt32(data["Angle"]);
|
|
}
|
|
else {
|
|
app.navModel.Angle = 0;
|
|
}
|
|
if (!string.IsNullOrEmpty(data["Distance"].ToString()))
|
|
{
|
|
app.navModel.Distance = Convert.ToInt32(data["Distance"]);
|
|
}
|
|
else {
|
|
app.navModel.Distance = 0;
|
|
}
|
|
if (!string.IsNullOrEmpty(data["ShopName"].ToString()))
|
|
{
|
|
app.navModel.ShopName = data["ShopName"].ToString();
|
|
}
|
|
else {
|
|
app.navModel.ShopName = "";
|
|
}
|
|
if (!string.IsNullOrEmpty(data["LogoPath"].ToString()))
|
|
{
|
|
app.navModel.LogoPath = dic["SenderID"].ToString().TrimEnd('/') + HttpUtility.UrlDecode(data["LogoPath"].ToString().Replace("\\", "/"));
|
|
}
|
|
else {
|
|
app.navModel.LogoPath = "";
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
//else if (type.Trim().Equals("appdown"))
|
|
//{
|
|
// app.AppDownCode = "";
|
|
// if (!string.IsNullOrEmpty(Commands["appid"].ToString()))
|
|
// {
|
|
// app.AppDownCode = Commands["appid"].ToString();
|
|
// }
|
|
//}
|
|
//else if (type.Trim().Equals("appupdate"))
|
|
//{
|
|
// app.AppUpdate = true;
|
|
// if (!string.IsNullOrEmpty(Commands["appname"].ToString()))
|
|
// {
|
|
// app.appName = Commands["appname"].ToString();
|
|
// app.Appdowninfo.exename = Commands["appname"].ToString();
|
|
// }
|
|
// if (!string.IsNullOrEmpty(Commands["filepath"].ToString()))
|
|
// {
|
|
// app.appUrl = dic["senderid"].ToString().TrimEnd('/') + Commands["filepath"].ToString();
|
|
|
|
// }
|
|
|
|
// if (!string.IsNullOrEmpty(Commands["appnameen"].ToString()))
|
|
// {
|
|
// app.Appdowninfo.exestartname = Commands["appnameen"].ToString();
|
|
// }
|
|
// if (!string.IsNullOrEmpty(Commands["iconfilepath"].ToString()))
|
|
// {
|
|
// app.icourl = dic["senderid"].ToString().TrimEnd('/') + Commands["iconfilepath"].ToString();
|
|
// }
|
|
// if (!string.IsNullOrEmpty(Commands["appid"].ToString()))
|
|
// {
|
|
// app.Appdowninfo.AppID = Commands["appid"].ToString();
|
|
// }
|
|
//}
|
|
//else if (type.Trim().Equals("appclock"))
|
|
//{
|
|
// if (!string.IsNullOrEmpty(Commands["data"].ToString()))
|
|
// {
|
|
// Dictionary<string, Object> data = JsonConvert.DeserializeObject<Dictionary<string, Object>>(Commands["data"].ToString());
|
|
// if (!string.IsNullOrEmpty(data["startapp"].ToString()))
|
|
// {
|
|
// Dictionary<string, string> startapp = JsonConvert.DeserializeObject<Dictionary<string, string>>(data["startapp"].ToString());
|
|
// if (!string.IsNullOrEmpty(startapp["filename"].ToString()))
|
|
// {
|
|
// app.AppPlayDir = startapp["filename"].ToString();
|
|
// app.AppStartinfo.exepath = startapp["appnameen"].ToString();
|
|
|
|
// if (!string.IsNullOrEmpty(startapp["appnameen"].ToString()))
|
|
// {
|
|
// //app.AppPlayName = startapp["appnameen"].ToString();
|
|
// app.AppStartinfo.exestartname = startapp["appnameen"].ToString();
|
|
// }
|
|
|
|
// if (!string.IsNullOrEmpty(startapp["appid"].ToString()))
|
|
// {
|
|
// app.AppStartinfo.AppID = startapp["appid"].ToString();
|
|
// }
|
|
// if (!string.IsNullOrEmpty(startapp["appname"].ToString()))
|
|
// {
|
|
// app.AppStartinfo.exename = startapp["appname"].ToString();
|
|
// }
|
|
// }
|
|
// else
|
|
// {
|
|
// if (!string.IsNullOrEmpty(data["endapp"].ToString()))
|
|
// {
|
|
// Dictionary<string, string> endapp = JsonConvert.DeserializeObject<Dictionary<string, string>>(data["endapp"].ToString());
|
|
// if (!string.IsNullOrEmpty(endapp["name"].ToString()))
|
|
// {
|
|
// app.AppStartinfo.exename = "";
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|
|
|
|
// }
|
|
//}
|
|
//else if (type.Trim().Equals("insertad"))
|
|
//{
|
|
// if (!string.IsNullOrEmpty(Commands["data"].ToString()))
|
|
// {
|
|
// Dictionary<string, Object> data = JsonConvert.DeserializeObject<Dictionary<string, Object>>(Commands["data"].ToString());
|
|
// if (!string.IsNullOrEmpty(data["filepath"].ToString()))
|
|
// {
|
|
// //Dictionary<string, string> admodel = JsonConvert.DeserializeObject<Dictionary<string, string>>(data["admodel"].ToString());
|
|
// //if (!string.IsNullOrEmpty(data["filepath"].ToString()))
|
|
// //{
|
|
// app.insertAD.Duration = Convert.ToInt32(data["duration"]);
|
|
// app.insertAD.FilePath = data["filepath"].ToString();
|
|
// //}
|
|
// }
|
|
|
|
// }
|
|
//}
|
|
//else if (type.Trim().Equals("addown"))
|
|
//{
|
|
// app.insertAD.Duration = 0;
|
|
// app.insertAD.FilePath = "";
|
|
//}
|
|
//else if (type.Trim().Equals("devnumedit"))
|
|
//{
|
|
// if (!string.IsNullOrEmpty(Commands["devnum"].ToString()))
|
|
// {
|
|
// app.devicemark = Commands["devnum"].ToString();
|
|
// }
|
|
//}
|
|
//else if (type.Trim().Equals("bgset"))
|
|
//{
|
|
// if (!string.IsNullOrEmpty(Commands["filepath"].ToString()))
|
|
// {
|
|
// app.bgPath = Commands["filepath"].ToString();
|
|
// }
|
|
//}
|
|
//else if (type.Trim().Equals("screensaver"))
|
|
//{
|
|
// app.screenTime = 30;
|
|
// if (!string.IsNullOrEmpty(Commands["time"].ToString()))
|
|
// {
|
|
// app.screenTime = Convert.ToInt32(Commands["time"]);
|
|
// }
|
|
//}
|
|
//else if (type.Trim().Equals("live"))
|
|
//{
|
|
// log.WriteLogFile(Commands["data"].ToString());
|
|
// if (!string.IsNullOrEmpty(Commands["data"].ToString()))
|
|
// {
|
|
// Dictionary<string, Object> data = JsonConvert.DeserializeObject<Dictionary<string, Object>>(Commands["data"].ToString());
|
|
// if (!string.IsNullOrEmpty(data["command"].ToString()))
|
|
// {
|
|
// if (data["command"].ToString() == "start")
|
|
// {
|
|
// app.mediaPlayerUrl = data["url"].ToString();
|
|
// }
|
|
// else
|
|
// {
|
|
// app.mediaPlayerUrl = "";
|
|
// }
|
|
// }
|
|
|
|
// }
|
|
//}
|
|
}
|
|
if (WebSocketReceiveEvent != null)
|
|
{
|
|
WebSocketReceiveEvent(type, "");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
log.WriteLogFile(ex.ToString(), "WebSocketLog");
|
|
|
|
}
|
|
}
|
|
|
|
void websocket_Error(object sender, SuperSocket.ClientEngine.ErrorEventArgs e)
|
|
{
|
|
|
|
log.WriteLogFile(e.Exception.Message + e.Exception.StackTrace, "WebSocketErrorLog");
|
|
//try
|
|
//{
|
|
// if (websocket != null)
|
|
// websocket.Close();
|
|
//}
|
|
//catch
|
|
//{
|
|
//}
|
|
//ReConnect();
|
|
}
|
|
|
|
/// <summary>
|
|
/// websocket重连
|
|
/// </summary>
|
|
private void ReConnect()
|
|
{
|
|
try
|
|
{
|
|
Action ac = new Action(() =>
|
|
{
|
|
while (true)
|
|
{
|
|
try
|
|
{
|
|
if (websocket.State == WebSocketState.Closed)
|
|
{
|
|
Thread.Sleep(30000);
|
|
|
|
if (websocket.State == WebSocketState.Closed)
|
|
websocket.Open();
|
|
}
|
|
|
|
if (websocket.State == WebSocketState.Open)
|
|
{
|
|
//reconnect = false;
|
|
break;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
log.WriteLogFile(ex.ToString(), "WebSocketErrorLog");
|
|
}
|
|
finally
|
|
{
|
|
Thread.Sleep(3000);
|
|
}
|
|
|
|
|
|
}
|
|
});
|
|
ac.BeginInvoke((o) =>
|
|
{
|
|
reconnect = false;
|
|
}, null);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
log.WriteLogFile(ex.ToString(), "WebSocketErrorLog");
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|
|
|