1. 程式人生 > >心跳包 WPF Unity 數據庫搭建

心跳包 WPF Unity 數據庫搭建

存在 rst host you userdata ops eem nts ati

1.心跳包如何發送:

主要是客戶機發送給服務器,服務器接收到後,再回復。

技術分享

//之前做的項目,WPF與U3D進行通信,WPF端是處於監聽端,而U3D屬於客戶端,不斷的發心跳包給WPF

//WPF端代碼如下:

//嵌入U3D窗口
void initU3D()
{
//將Exehost 控件添加到 Winformhost控件中
if (UserData.enableEmbU3d)
{
ExeHost exehost = new ExeHost();

//formview.formHost.Child = exehost;
formHost.Child = exehost;
exehost.Hold(Window_Main.mainu3dpath);
}
netparser = new Parser(this);
U3DMgr.SetCallBack(netparser);
}
Parser netparser;
ProgressBar progressbar;
public class Parser : u3dconnect.IParser
{

Page_MakeUp_Main page;
public Parser(Page_MakeUp_Main page)
{
this.page = page;
}

public void OnHttpReq(string req, out u3dconnect.returninfo returndata)
{
throw new NotImplementedException();
}

public void OnUdpRecv(MyJson.JsonNode_Object json)
{

page.onRecvUdp(json);
}
}
bool bU3dInited = false;
bool bLoadScene = false;
bool m_isPicShow = false;


public void onRecvUdp(MyJson.JsonNode_Object json)//接收到unity發來的消息之後,再發命令過去
{


if (json["module"].AsString() == "makeup")//是我要的模塊
{

//waitwindow.uiupdate();

//System.Diagnostics.Debug.WriteLine("window_wait: Hide ");
waitwindow.Hide();//切換場景成功,隱藏等待界面
if (bU3dInited == false || json["cmd"].AsString() == "init")
{
bU3dInited = true;
MyJson.JsonNode_Object loadlayout = new MyJson.JsonNode_Object();//load房型
if (UserData.Data["scenetype"].AsInt() == (int)SceneType.scene)
{
//這裏已經切好場景了
loadlayout.SetDictValue("module", "makeup");
loadlayout.SetDictValue("cmd", "config_layout");
loadlayout.SetDictValue("userName", UserData.Data.asDict()["userName"].AsString());
loadlayout.SetDictValue("token", UserData.Data.asDict()["token"].AsString());
//loadlayout.SetDictValue("defaultlayer", UserData.Data["defaultlayer"].ToString());
//還要再次出傳過去一個字段,表示是創建一個裝修還是打開一個已經存在的裝修
loadlayout.SetDictValue("editmode", UserData.Data["EditMode"].ToString());

U3DMgr.UdpSend(loadlayout.ToString());
}
else
{
//載入房體
loadlayout.SetDictValue("module", "makeup");
loadlayout.SetDictValue("cmd", "load_layout");//這個也要改,不同的層,發不同的層的名字過去,初始化,發的是第一層
loadlayout.SetDictValue("indexfile", UserData.Data["houseLayoutIndex"]);//進到這裏,自然就獲取到了這個index
loadlayout.SetDictValue("userName", UserData.Data.asDict()["userName"].AsString());
loadlayout.SetDictValue("token", UserData.Data.asDict()["token"].AsString());
loadlayout.SetDictValue("defaultlayer", UserData.Data["defaultlayer"].ToString());
//還要再次出傳過去一個字段,表示是創建一個裝修還是打開一個已經存在的裝修
loadlayout.SetDictValue("editmode", UserData.Data["EditMode"].ToString());

U3DMgr.UdpSend(loadlayout.ToString());
}


string indexfile = UserData.Data["makeupIndexFile"].ToString();//這個是判斷點擊的是
if (indexfile != "" && indexfile != "-1")
{
//再次置為空

MyJson.JsonNode_Object load = new MyJson.JsonNode_Object();//load裝修
loadlayout.SetDictValue("module", "makeup");
loadlayout.SetDictValue("cmd", "load");
loadlayout.SetDictValue("indexfile", UserData.Data["makeupIndexFile"]);
loadlayout.SetDictValue("userName", UserData.Data.asDict()["userName"].AsString());
loadlayout.SetDictValue("token", UserData.Data.asDict()["token"].AsString());
U3DMgr.UdpSend(loadlayout.ToString());
//看看有沒有可載入的裝修
}

}
if (json["cmd"].AsString() == "updateindexfile")
{
UserData.Data["makeupIndexFile"] = new MyJson.JsonNode_ValueString(json["indexfile"].AsString());//這個是存的裝修的index文件
UserData.Data["makeupPicIndexFile"] = new MyJson.JsonNode_ValueString(json["picIndex"].AsString());//這個是存的裝修的picIndex
UserData.Data["isfix"] = new MyJson.JsonNode_ValueNumber(json["isFixed"].AsInt());//這個是存的裝修的picIndex
if (needtosave)
{
onNeedSave();
needtosave = false;
}
}

if (json["cmd"].AsString() == "resetindex")
{

if (progressbar != null)
{
progressbar.Visibility = System.Windows.Visibility.Hidden;
progressbar = null;
}
//initLayout(ItemContainer);//重新刷新數據

listpanel.ListBox_Furniture_List.SelectedIndex = -1;

if (windowstate == WindowState.panellist)
{
listpanel.Visibility = System.Windows.Visibility.Visible;
}
if (windowstate == WindowState.panelinfo)
{
panelInfo.Visibility = System.Windows.Visibility.Visible;
}
//Grid_MakeUp.Visibility = System.Windows.Visibility.Visible;
if (houselayoutTemp != null)
{
houselayoutTemp.Image_Download.Visibility = System.Windows.Visibility.Hidden;
houselayoutTemp = null;
}
}

if (json["cmd"].AsString() == "price")//這個用在,獲取到裝修價格後,在本地顯示
{


if (progressbar != null)
{
progressbar.Visibility = System.Windows.Visibility.Hidden;
progressbar = null;
}
//initLayout(ItemContainer);//重新刷新數據

if (windowstate == WindowState.panellist)
{
listpanel.Visibility = System.Windows.Visibility.Visible;
}
if (windowstate == WindowState.panelinfo)
{
panelInfo.Visibility = System.Windows.Visibility.Visible;
}
UserControlMakeUpMenuSecond.Label_AllPrice.Content = json["makeup_price"].ToString();
listpanel.ListBox_Furniture_List.SelectedIndex = -1;

if (houselayoutTemp != null)
{
houselayoutTemp.Image_Download.Visibility = System.Windows.Visibility.Hidden;
houselayoutTemp = null;
}

}
if (json["cmd"].AsString() == "requestprice")//這個用在像u3d端請求裝修價格
{
MyJson.JsonNode_Object news = new MyJson.JsonNode_Object();
news.SetDictValue("module", "makeup");
news.SetDictValue("cmd", "getprice");
U3DMgr.UdpSend(news.ToString());
}
if (json["cmd"].AsString() == "progressbar")//進度條增加
{
if (progressbar == null)
return;

if (windowstate == WindowState.panellist)
{
if (listpanel.Visibility == System.Windows.Visibility.Visible)
{
if (progressbar.Value >= 95)
{
progressbar.Value = 95;

if (progressbar != null)
{
progressbar.Visibility = System.Windows.Visibility.Hidden;
//progressbar = null;
}
}
else
{
progressbar.Value += 5;

}
}
}
if (windowstate == WindowState.panelinfo)
{
if (panelInfo.Visibility == System.Windows.Visibility.Visible)
{
if (progressbar.Value >= 95)
{
progressbar.Value = 95;

if (progressbar != null)
{
progressbar.Visibility = System.Windows.Visibility.Hidden;
//progressbar = null;
}

}
else
{
progressbar.Value += 5;
}
}
}


}
if (json["cmd"].AsString() == "progressbarfull")//進度條變滿
{
if (progressbar != null)
{
progressbar.Visibility = System.Windows.Visibility.Hidden;
//progressbar = null;
}


if (windowstate == WindowState.panellist)
{
if (listpanel.Visibility == System.Windows.Visibility.Visible)
{
listpanel.Visibility = System.Windows.Visibility.Hidden;

//progressbar.Value = 0;
}

}
if (windowstate == WindowState.panelinfo)
{
if (panelInfo.Visibility == System.Windows.Visibility.Visible)
{

panelInfo.Visibility = System.Windows.Visibility.Hidden;

}
}


}
if (json["cmd"].AsString() == "notneedsave")
{
Uri name1 = new Uri("ui/Page_Main_MakeUp2.xaml", UriKind.Relative);
NavigationService.Navigate(name1);
}
if (json["cmd"].AsString() == "needsave")
{
//彈出對話框
Save_Layout save_makeup = new Save_Layout();
save_makeup.ShowDialog();
if (save_makeup.state == Save_Layout.State.yes)
{
//保存
//這裏保存分為兩種,一種是需要填名字的,另一種是不需要填名字的
SaveMakeUpQuit();
}
else if (save_makeup.state == Save_Layout.State.no)
{
//不保存
Uri name1 = new Uri("ui/Page_Main_MakeUp2.xaml", UriKind.Relative);
NavigationService.Navigate(name1);
}
}
if (json["cmd"].AsString() == "savequit")
{
UserData.Data["makeupIndexFile"] = new MyJson.JsonNode_ValueString(json["indexfile"].AsString());//這個是存的裝修的index文件
UserData.Data["makeupPicIndexFile"] = new MyJson.JsonNode_ValueString(json["picIndex"].AsString());//這個是存的裝修的picIndex
UserData.Data["isfix"] = new MyJson.JsonNode_ValueNumber(json["isFixed"].AsInt());//這個是存的裝修的picIndex
Console.WriteLine(UserData.Data["isfix"]);
if (needtosave)
{
onNeedSave();
needtosave = false;
}

Uri name1 = new Uri("ui/Page_Main_MakeUp2.xaml", UriKind.Relative);
NavigationService.Navigate(name1);
}

if (json["cmd"].AsString() == "getpanoramadone")
{
UserData.picHash = json["pic"].AsString();//全景圖的hash 作為縮略圖索引
UserData.picIndex = json["index"].AsInt();//全景圖的key
System.Diagnostics.Debug.WriteLine("all pic UserData.picHash : " + UserData.picHash);
if (!m_isPicShow)
{
if (winAnimation != null && winAnimation.IsEnd)
{
this.UpdateAllPicList();
}
}

}
if (json["cmd"].AsString() == "getpanoramatip")
{
PopSure popsure = new PopSure();
popsure.Label_Tip.Content = "此位置已經拍攝過照片,請換個位置再拍。";
popsure.Label_Tip.FontSize = 12;
popsure.ShowDialog();
//MessageBox.Show("此位置已經拍攝過照片,請換個位置再拍。");
}
if (json["cmd"].AsString() == "pricesheet")
{
//MessageBox.Show(json["info"].ToString());
MyJson.JsonNode_Array obj = (MyJson.Parse(json["info"].ToString()) as MyJson.JsonNode_Object)["MakeUpPrice"] as MyJson.JsonNode_Array;
rf.SetGrid(obj);
//rf = new ReportForm(json["info"] as MyJson.JsonNode_Array);
}

if (json["cmd"].AsString() == "getcapturecameradone")
{
UserData.picHash = json["pic"].AsString();
UserData.picIndex = json["index"].AsInt();
System.Diagnostics.Debug.WriteLine("one pic UserData.picHash : " + UserData.picHash);
if (!m_isPicShow)
{
if (winAnimation != null && winAnimation.IsEnd)
{
this.UpdateSinglePicList();


}
}
}
//在這裏加個消息事件,那邊只要有mousebuttondown過來,就把彈出的框close掉
if (json["cmd"].AsString() == "closeMorePopUp")
{
if (_screenListStore != null)
{
_screenListStore.Close();
}
//MessageBox.Show("關閉");
}
//if (json.ContainsKey("classindex") == false) return; //沒有分類號,return
//Console.WriteLine("editmodel udp recv:" + json.ToString());
}
else//不是編輯模塊,就改變場景
{
if (bLoadScene) return;//不允許多次發送切換場景命令
MyJson.JsonNode_Object changeto = new MyJson.JsonNode_Object();
if (UserData.Data["scenetype"].AsInt() == (int)SceneType.scene)
{
changeto.SetDictValue("module", "null");
changeto.SetDictValue("cmd", "changescenefix");
changeto.SetDictValue("sceneindex", UserData.Data["houseLayoutIndex"]);
changeto.SetDictValue("scene", "makeup");
}
else
{
changeto.SetDictValue("module", "null");
changeto.SetDictValue("cmd", "changescene");
changeto.SetDictValue("scene", "makeup");
}
bLoadScene = true;
U3DMgr.UdpSend(changeto.ToString());
//Console.WriteLine("editmodel udp recv:" + json.ToString());
}
}

//U3D的代碼如下:

//在U3D的入口代碼中,Update函數,每三秒鐘像WPF發送一次消息

void Update()
{
if(bSleep)
{
System.Threading.Thread.Sleep(100);
}
if (nettools != null)
{
nettools.Update();//檢測下載
}
Connect.Instance.checkCallback();//這裏需要檢查回調 實時更新的檢查回調
fcount++;
//定時發送心跳包
timer += Time.deltaTime;
if (timer > 3.0f)
{
timer = 0;
fcount = 0;
MyJson.JsonNode_Object beatheart = new MyJson.JsonNode_Object();
beatheart.SetDictValue("module", modulename);
beatheart.SetDictValue("cmd", "beatheart");
Connect.Instance.udpSend(beatheart);
}
if (curState != null)
{
curState.OnUpdate(Time.deltaTime);
}
}

心跳包 WPF Unity 數據庫搭建