using BestHTTP;
using BestHTTP.JSON;
using EasyInject.Attributes;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
///
/// 提供网络服务
///
[Component]
public class NetWorkComponent
{
///
/// 发送 post content-type = 'application/json'
///
///
///
///
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;
}
///
/// 发送 post content-type = 'application/json'
///
///
///
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;
}
///
/// 发送get 请求
///
///
///
///
public HTTPRequest Get(string url,OnRequestFinishedDelegate callBack)
{
HTTPRequest get = HTTPManager.SendRequest(url, HTTPMethods.Get,false, callBack);
get.Send();
return get;
}
///
/// 连接所有的websocket通信
///
public void ConnectAllWebSocket()
{
List webSocketChannles = ApplicationBoot.Instance.GetBeans();
foreach(var websocket in webSocketChannles)
{
try
{
websocket.Connect();
}catch (Exception ex)
{
Debug.LogError(ex);
}
}
}
///
/// websocket 所有连接已建立
///
public bool AllWebSocketConnected()
{
List webSocketChannles = ApplicationBoot.Instance.GetBeans();
bool connected = false;
foreach (var websocket in webSocketChannles)
{
connected = websocket.IsConnected;
}
return connected;
}
}