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);
}
}