89 lines
2.7 KiB
C#
89 lines
2.7 KiB
C#
using BestHTTP;
|
|
using BestHTTP.JSON;
|
|
using EasyInject.Attributes;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using UnityEngine;
|
|
/// <summary>
|
|
/// 提供网络服务
|
|
/// </summary>
|
|
[Component]
|
|
public class NetWorkComponent
|
|
{
|
|
|
|
/// <summary>
|
|
/// 发送 post content-type = 'application/json'
|
|
/// </summary>
|
|
/// <param name="url"></param>
|
|
/// <param name="param"></param>
|
|
/// <param name="callBack"></param>
|
|
public HTTPRequest PostJson(string url,object param, OnRequestFinishedDelegate callBack)
|
|
{
|
|
HTTPRequest post = HTTPManager.SendRequest(url, HTTPMethods.Post, true, callBack);
|
|
string json = JsonConvert.SerializeObject(param);
|
|
post.RawData = Encoding.UTF8.GetBytes(json);
|
|
post.SetHeader("Content-Type", "application/json");
|
|
post.SetHeader("Content-Length", post.RawData.Length.ToString());
|
|
post.Send();
|
|
return post;
|
|
}
|
|
/// <summary>
|
|
/// 发送 post content-type = 'application/json'
|
|
/// </summary>
|
|
/// <param name="url"></param>
|
|
/// <param name="param"></param>
|
|
public HTTPRequest PostJson(string url, object param)
|
|
{
|
|
HTTPRequest post = HTTPManager.SendRequest(url, HTTPMethods.Post, false, null);
|
|
post.RawData = Encoding.UTF8.GetBytes(JsonUtility.ToJson(param));
|
|
post.SetHeader("Content-Type", "application/json");
|
|
post.SetHeader("Content-Length", post.RawData.Length.ToString());
|
|
post.Send();
|
|
return post;
|
|
}
|
|
/// <summary>
|
|
/// 发送get 请求
|
|
/// </summary>
|
|
/// <param name="url"></param>
|
|
/// <param name="param"></param>
|
|
/// <param name="callBack"></param>
|
|
public HTTPRequest Get(string url,OnRequestFinishedDelegate callBack)
|
|
{
|
|
HTTPRequest get = HTTPManager.SendRequest(url, HTTPMethods.Get,false, callBack);
|
|
get.Send();
|
|
return get;
|
|
}
|
|
/// <summary>
|
|
/// 连接所有的websocket通信
|
|
/// </summary>
|
|
public void ConnectAllWebSocket()
|
|
{
|
|
List<IWebSocketChannle> webSocketChannles = ApplicationBoot.Instance.GetBeans<IWebSocketChannle>();
|
|
foreach(var websocket in webSocketChannles)
|
|
{
|
|
try
|
|
{
|
|
websocket.Connect();
|
|
}catch (Exception ex)
|
|
{
|
|
Debug.LogError(ex);
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// websocket 所有连接已建立
|
|
/// </summary>
|
|
public bool AllWebSocketConnected()
|
|
{
|
|
List<IWebSocketChannle> webSocketChannles = ApplicationBoot.Instance.GetBeans<IWebSocketChannle>();
|
|
bool connected = false;
|
|
foreach (var websocket in webSocketChannles)
|
|
{
|
|
connected = websocket.IsConnected;
|
|
}
|
|
return connected;
|
|
}
|
|
}
|