110 lines
2.9 KiB
C#
110 lines
2.9 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Text;
|
||
using UnityEngine;
|
||
using UnityEngine.SceneManagement;
|
||
using System.Linq;
|
||
using System.Collections;
|
||
using System.IO;
|
||
using Newtonsoft.Json.Linq;
|
||
|
||
public class LoadManage : MonoBehaviour
|
||
{
|
||
public static LoadManage Instance;
|
||
|
||
/// <summary>
|
||
/// 房间域 "Room"
|
||
/// </summary>
|
||
[DisplayOnly]
|
||
public string currentRoomArea;
|
||
[DisplayOnly]
|
||
public MyNetMQClient RSclient;
|
||
|
||
public Action<byte[]> reciveData;
|
||
private void Awake()
|
||
{
|
||
Instance = this;
|
||
DontDestroyOnLoad(gameObject);
|
||
|
||
|
||
#if UNITY_EDITOR
|
||
UnityEngine.Debug.unityLogger.logEnabled = true;
|
||
#else
|
||
UnityEngine.Debug.unityLogger.logEnabled = false;
|
||
#endif
|
||
}
|
||
void Start()
|
||
{
|
||
//设置不休眠
|
||
Screen.sleepTimeout = SleepTimeout.NeverSleep;
|
||
string ip = "";
|
||
//配置
|
||
File.ReadAllLines(Application.streamingAssetsPath + "/MainSetting.txt").ToList().ForEach(line =>
|
||
{
|
||
if (line.Trim(' ').StartsWith("serverIP="))
|
||
{
|
||
string[] tmps = line.Trim(' ').Split('=');
|
||
ip = tmps[1];
|
||
}
|
||
});
|
||
CreateRoomServerClient(string.Format("tcp://{0}:8889", ip), string.Format("tcp://{0}:8888", ip));
|
||
//MyNetMQClient.instance._netMqListener.SubTopic("Room");
|
||
SceneManager.LoadScene("SampleScene");
|
||
}
|
||
|
||
/// <summary>
|
||
/// 创建与roomServer通信的客户端
|
||
/// </summary>
|
||
public void CreateRoomServerClient(string roomServerSubIP, string roomServerPubIP)
|
||
{
|
||
if (RSclient == null)
|
||
{
|
||
//测试
|
||
//roomServerPubIP = "tcp://192.168.1.125:56987";
|
||
// roomServerSubIP = "tcp://192.168.1.125:56991";
|
||
RSclient = gameObject.AddComponent<MyNetMQClient>();
|
||
RSclient.Init(roomServerPubIP, roomServerSubIP, ReciveFromRoomServerInThread, ReciveFromRoomServerInMono, "roomServer");
|
||
Debug.Log("开启roomServer:" + roomServerPubIP + "------" + roomServerSubIP);
|
||
}
|
||
}
|
||
|
||
public void RemoveRoomServerClient()
|
||
{
|
||
if (RSclient != null)
|
||
{
|
||
RSclient.Destroy();
|
||
// RSclient = null;
|
||
Debug.Log("RSclient置空");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// RoomServer,mono
|
||
/// </summary>
|
||
/// <param name="stS"></param>
|
||
private void ReciveFromRoomServerInThread(st_Motions stS)
|
||
{
|
||
RSclient._netMqListener.AddToMono(stS);
|
||
}
|
||
|
||
/// <summary>
|
||
/// RoomServer,mono
|
||
/// </summary>
|
||
/// <param name="data"></param>
|
||
public void ReciveFromRoomServerInMono(st_Motions stS)
|
||
{
|
||
if (reciveData != null)
|
||
{
|
||
reciveData(stS.m_sOperaData);
|
||
}
|
||
}
|
||
|
||
private void OnDestroy()
|
||
{
|
||
Debug.Log("OnDestroy");
|
||
if (RSclient != null)
|
||
{
|
||
RSclient.Destroy();
|
||
}
|
||
}
|
||
} |