ict.shenzhi/Assets/ZHYscrip/scrip/WebSocketMessageHandler.cs

156 lines
6.9 KiB
C#

using Newtonsoft.Json.Linq;
using PimDeWitte.UnityMainThreadDispatcher;
using System;
using UnityEngine;
using UnityEngine.UI;
public class WebSocketMessageHandler : MonoBehaviour
{
// public buttonFunctionAdd buttonFunctionAdd_;
void Start()
{
// 订阅 WebSocketServerManager 的信息接收事件
WebSocketServerManager.Instance.OnMessageReceived += HandleWebSocketMessage;
}
private void Update()
{
}
// 处理接收到的 WebSocket 信息
private void HandleWebSocketMessage(ToolsEventModel eventModel)
{
//根据 eventType 判断事件类型并执行相应的处理
UnityMainThreadDispatcher.Instance().Enqueue(() =>
{
Debug.Log(eventModel.eventType);
switch (eventModel.eventType)
{
case "Overview": //概览
Debug.Log("接收到 Overview 事件数据: " + eventModel.data);
//可以在这里处理接收到的 Overview 事件的数据
break;
case "IDESettings": //IDE 设置
Debug.Log("接收到 IDESettings 事件数据: " + eventModel.data);
//使用 JObject 解析 JSON 字符串
JObject jsonObject = JObject.Parse((string)eventModel.data);
//获取 "IdePath" 和 "IdeTypeEnum" 的值
string idePath = jsonObject["IdePath"].ToString();
int ideTypeEnum = jsonObject["IdeTypeEnum"].ToObject<int>();
//输出调试信息
Console.WriteLine("接收到的 IDE 配置:");
Console.WriteLine("IdePath: " + idePath);
Console.WriteLine("IdeTypeEnum: " + ideTypeEnum);
//根据 ideTypeEnum 的值来设置相应的 IDE 路径
switch (ideTypeEnum)
{
case 0:
PlayerPrefs.SetString("pythonPath", idePath);
break;
case 1:
PlayerPrefs.SetString("javaPath", idePath);
break;
case 2:
PlayerPrefs.SetString("cPath", idePath);
break;
}
//根据 IDE 设置启动相应的编辑器
Debug.Log(ideTypeEnum);
//MainCanvasManager.iDEsettingPanel.RunEditor(ideTypeEnum);
break;
case "SceneReset": //场景重置
Debug.Log("接收到 SceneReset 事件数据: " + eventModel.data);
Debug.Log("场景重置");
//MainCanvasManager.menu_panel.OnResetScene();
break;
case "ScreenRecording": //屏幕录制
Debug.Log("接收到 ScreenRecording 事件数据: " + eventModel.data);
if ((bool)eventModel.data)
{
Debug.Log("开始录制");
//MainCanvasManager.menu_panel.StartVideoRecord();
}
else
{
Debug.Log("停止录制");
//MainCanvasManager.menu_panel.StopVideoRecord();
}
break;
case "TemporaryStorage": //临时存储
Debug.Log("接收到 TemporaryStorage 事件数据: " + eventModel.data);
Debug.Log("数据暂存");
//ConsolePanel.ConsoleOutput("接收到:数据暂存", "log");
//MainCanvasManager.menu_panel.OnStaging("暂存");
//GameObject.Find("All").transform.GetChild(1).GetChild(0).GetChild(3).GetComponent<buttonFunctionAdd>().OldFunction.OnStaging("暂存");
GameObject.Find("Mini").GetComponent<MenuPanel>().OnStaging("暂存");
Debug.Log("数据暂存完成");
break;
case "DataReading": // 数据读取
Debug.Log("接收到 DataReading 事件数据: " + eventModel.data);
Debug.Log("读取");
//ConsolePanel.ConsoleOutput("接收到:读取", "log");
//GameObject.Find("All").transform.GetChild(1).GetChild(0).GetChild(3).GetComponent<buttonFunctionAdd>().OldFunction.DownLoadProject();
GameObject.Find("Mini").GetComponent<MenuPanel>().OnDownload();
//MainCanvasManager.menu_panel.OnDownload();
break;
case "submit"://提交功能
Debug.Log("接收到 TemporaryStorage 事件数据: " + eventModel.data);
Debug.Log("提交");
//ConsolePanel.ConsoleOutput("接收到:提交", "log");
//MainCanvasManager.menu_panel.OnStaging("暂存");
//GameObject.Find("All").transform.GetChild(1).GetChild(0).GetChild(3).GetComponent<buttonFunctionAdd>().OldFunction.OnSubmit();
GameObject.Find("Mini").GetComponent<MenuPanel>().OnSubmit();
Debug.Log("数据提交完成");
break;
case "StartExperiment": //开始实验
Debug.Log("接收到 StartExperiment 事件数据: " + eventModel.data);
//youxiajiao.Instance.Cancel();
break;
case "MiniModel": //Mini 模式
//MainCanvasManager.Instance.MinoMode();
Debug.Log("切换mini");
break;
case "Thumbnail": //缩小视图
//WindowControl.Instance.IsNarrow();
Debug.Log("缩小视图");
break;
case "HuiFu": //恢复界面
//WindowControl.Instance.Reduction();
//UIMgr.Instance.Open();
//GameObject.Find("All").transform.GetChild(1).GetChild(0).GetChild(3).GetComponent<buttonFunctionAdd>().Function_切换max();
buttonFunctionAdd.Instance.Function_切换max();
//ConsolePanel.ConsoleOutput("接收到:切换", "log");
Debug.Log("恢复");
break;
case "closeapp":
WebSocketServerManager.Instance.end_();
MyServer.instance.StopWebsocketServer(10088); ;
Application.Quit();
break;
default:
Debug.LogWarning("未知的事件类型: " + eventModel.eventType);
break;
}
});
}
// 确保在销毁时取消订阅事件
private void OnDestroy()
{
WebSocketServerManager.Instance.OnMessageReceived -= HandleWebSocketMessage;
}
}