270 lines
7.8 KiB
C#
270 lines
7.8 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
using System;
|
||
using System.Collections.Concurrent;
|
||
using System.Linq;
|
||
using System.Diagnostics;
|
||
using UnityEngine.Networking;
|
||
using DataModel.Model;
|
||
using System.Net;
|
||
using System.Net.Sockets;
|
||
using System.Text;
|
||
using System.Threading;
|
||
|
||
public class MyNetMQClient : MonoBehaviour
|
||
{
|
||
public static MyNetMQClient instance;
|
||
[DisplayOnly]
|
||
public string PubIP= "127.0.0.1:8082";
|
||
[DisplayOnly]
|
||
public string SubIP = "127.0.0.1:8081";
|
||
|
||
/// <summary>
|
||
/// 接口IP (配置文件)
|
||
/// </summary>
|
||
public static string CallIP = "192.168.0.103:8080";
|
||
public static string SyncServerIP = "192.168.0.103:8888";
|
||
public static string remoteServerIP = "192.168.0.103:8890";
|
||
public static string userIP = "192.168.0.102";
|
||
|
||
[HideInInspector]
|
||
public NetMqListener _netMqListener;
|
||
[HideInInspector]
|
||
public NetMqPublisher _netMqPublisher;
|
||
|
||
|
||
|
||
private void Awake()
|
||
{
|
||
instance = this;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 初始化Netmq
|
||
/// </summary>
|
||
/// <param name="reciveInMono">接收函数</param>
|
||
public void Init(string ServerSubIP,string ServerPubIP,string area, NetMqListener.ReciveMessageInThread reciveInthread, NetMqListener.ReceiceMessageInMono reciveInMono,string typename)
|
||
{
|
||
SubIP = ServerSubIP;
|
||
PubIP = ServerPubIP;
|
||
//开启接收模块
|
||
_netMqListener = new NetMqListener(ServerSubIP, area, reciveInthread, reciveInMono);
|
||
_netMqListener.typeName = typename;
|
||
//开启发送模块
|
||
_netMqPublisher = new NetMqPublisher(ServerPubIP);
|
||
_netMqPublisher.typeName = typename;
|
||
}
|
||
|
||
public void Send(string area,int type,byte[] data)
|
||
{
|
||
_netMqPublisher.AddMessageToSendQue(area, type, data);
|
||
}
|
||
public void Send(st_Motions st)
|
||
{
|
||
_netMqPublisher.AddMessageToSendQue(st);
|
||
}
|
||
|
||
private void Update()
|
||
{
|
||
if (_netMqListener != null)
|
||
{
|
||
_netMqListener.UpdateByte();
|
||
}
|
||
|
||
//测试
|
||
//if(Input.GetKeyDown(KeyCode.Space))
|
||
//{
|
||
// Send(LoadManage.Instance.currentPractice.RoomArea, 1000, new byte[5]);
|
||
//}
|
||
}
|
||
|
||
public void Destroy()
|
||
{
|
||
_netMqListener._listenerCancelled = true;
|
||
_netMqPublisher._listenerCancelled = true;
|
||
UnityEngine.Debug.Log("销毁xxxxx");
|
||
Destroy(this);
|
||
}
|
||
|
||
|
||
#region 调接口
|
||
/// <summary>
|
||
/// 调接口
|
||
/// </summary>
|
||
/// <param name="url"></param>
|
||
/// <param name="datas"></param>
|
||
/// <param name="back"></param>
|
||
/// <returns></returns>
|
||
public static IEnumerator CallPost(string url, KeyValuePair<string, string>[] datas, Action<string> back)
|
||
{
|
||
WWWForm form = new WWWForm();
|
||
for (int i = 0; i < datas.Length; i++)
|
||
{
|
||
form.AddField(datas[i].Key, datas[i].Value);
|
||
}
|
||
UnityWebRequest request = UnityWebRequest.Post(url, form);
|
||
yield return request.SendWebRequest();
|
||
if (request.isDone)
|
||
{
|
||
if (request.isHttpError || request.isNetworkError)
|
||
{
|
||
MessagePanel.ShowMessage("接口异常,isHttpError"+ request.isHttpError+ ".isNetworkError"+ request.isNetworkError, GameObject.FindObjectOfType<Canvas>().transform);
|
||
}
|
||
else
|
||
{
|
||
if (back != null)
|
||
{
|
||
back.Invoke(request.downloadHandler.text);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 调接口
|
||
/// </summary>
|
||
/// <param name="url"></param>
|
||
/// <param name="back"></param>
|
||
/// <returns></returns>
|
||
public static IEnumerator CallGet(string url, Action<string> back)
|
||
{
|
||
UnityWebRequest request =UnityWebRequest.Get(url);
|
||
yield return request.SendWebRequest();
|
||
if (request.isDone)
|
||
{
|
||
if (request.isHttpError || request.isNetworkError)
|
||
{
|
||
MessagePanel.ShowMessage("接口异常+isHttpError"+ request.isHttpError+ ",isNetworkError"+ request.isNetworkError, GameObject.FindObjectOfType<Canvas>().transform);
|
||
}
|
||
else
|
||
{
|
||
if (back != null)
|
||
{
|
||
back.Invoke(request.downloadHandler.text);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
public static IEnumerator GetFile(string url,Action<byte[]> back)
|
||
{
|
||
UnityWebRequest request = UnityWebRequest.Get(url);
|
||
yield return request.SendWebRequest();
|
||
if (request.isDone)
|
||
{
|
||
if (request.isHttpError || request.isNetworkError)
|
||
{
|
||
MessagePanel.ShowMessage("接口异常+isHttpError" + request.isHttpError + ",isNetworkError" + request.isNetworkError, GameObject.FindObjectOfType<Canvas>().transform);
|
||
}
|
||
else
|
||
{
|
||
if (back != null)
|
||
{
|
||
back.Invoke(request.downloadHandler.data);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 调用web接口(PostUploadHandler)
|
||
/// </summary>
|
||
/// <param name="url"></param>
|
||
/// <param name="json">数据</param>
|
||
/// <param name="callback">回调</param>
|
||
/// <returns></returns>
|
||
public static IEnumerator InvokeWebPostByUploadhandler(string url, string json, Action<bool,string> callback)
|
||
{
|
||
byte[] uploadData = Encoding.UTF8.GetBytes(json);
|
||
UnityWebRequest webRequest = new UnityWebRequest(url, "POST");
|
||
webRequest.SetRequestHeader("accept", "*/*");
|
||
webRequest.SetRequestHeader("contentType", "application/json");
|
||
webRequest.downloadHandler = new DownloadHandlerBuffer();
|
||
webRequest.uploadHandler = new UploadHandlerRaw(uploadData);
|
||
|
||
yield return webRequest.SendWebRequest();
|
||
if (webRequest.isHttpError || webRequest.isNetworkError)
|
||
{
|
||
callback(false,webRequest.error);
|
||
}
|
||
else
|
||
{
|
||
callback(true,webRequest.downloadHandler.text);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 上传文件
|
||
/// </summary>
|
||
/// <param name="url"></param>
|
||
/// <param name="file"></param>
|
||
/// <param name="fileName"></param>
|
||
/// <param name="back"></param>
|
||
/// <returns></returns>
|
||
public static IEnumerator UpLoadFile(string url,byte[] file,string fileName,Action<CallResultObject> back)
|
||
{
|
||
WWWForm form = new WWWForm();
|
||
form.AddField("action", "Add");
|
||
form.AddField("saveName", fileName);
|
||
form.AddBinaryData("file", file);
|
||
|
||
UnityWebRequest request = UnityWebRequest.Post(url, form);
|
||
yield return request.SendWebRequest();
|
||
if (request.isDone)
|
||
{
|
||
if (request.isHttpError || request.isNetworkError)
|
||
{
|
||
MessagePanel.ShowMessage("接口异常,isHttpError" + request.isHttpError + ".isNetworkError" + request.isNetworkError, GameObject.FindObjectOfType<Canvas>().transform);
|
||
back(new CallResultObject { state = false });
|
||
}
|
||
else
|
||
{
|
||
if (back != null)
|
||
{
|
||
string str=request.downloadHandler.text;
|
||
CallResultObject co= LitJson.JsonMapper.ToObject<CallResultObject>(str);
|
||
back.Invoke(co);
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
back(new CallResultObject { state = false });
|
||
}
|
||
}
|
||
#endregion
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
/// <summary>
|
||
/// 消息体
|
||
/// </summary>
|
||
[Serializable]
|
||
public struct st_Motions
|
||
{
|
||
/// <summary>
|
||
/// 域
|
||
/// </summary>
|
||
public string area;
|
||
/// <summary>
|
||
/// 消息标识
|
||
/// </summary>
|
||
public int m_iOperaType;
|
||
/// <summary>
|
||
/// 消息内容(byte[256])
|
||
/// </summary>
|
||
public byte[] m_sOperaData;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 演习状态
|
||
/// </summary>
|
||
public enum ProgramState
|
||
{
|
||
结束,
|
||
进行中
|
||
}
|