示例#1
0
        static IEnumerator Connect(string url, MyActionOne action = null, WWWForm form = null)
        {
            UnityWebRequest web = null;

            if (form == null)
            {
                web = UnityWebRequest.Get(url);
            }
            else
            {
                web = UnityWebRequest.Post(url, form);
            }
            web.timeout = 30;
            yield return(web.SendWebRequest());

            if (web.isNetworkError)//超时
            {
                Debug.Log("连接超时");
            }
            else if (web.isHttpError)//其它情况
            {
                Debug.Log("连接出错: " + web.error);
            }
            else
            {
                if (action != null)
                {
                    action(web.downloadHandler.text);
                    action = null;
                }
            }
            web = null;
        }
示例#2
0
 public void ConnectTo(string ip, int port, MyActionOne connOK = null, MyActionOne connNo = null, int outTimer = 10)
 {
     try
     {
         if (isClosed)
         {
             this.initSocket();
         }
         if (isConnection)
         {
             if (connOK != null)
             {
                 connOK("-1");                //没有连接
             }
             return;
         }
         //2:设置连接 到服务器的IP和端口
         IPEndPoint IpPort = new IPEndPoint(IPAddress.Parse(ip), port);
         //3:开始连接
         if (this.socket.BeginConnect(IpPort, null, this.socket).AsyncWaitHandle.WaitOne(outTimer * 1000))
         {
             if (this.socket.Connected)
             {
                 this.isConnection = true;
                 this.BeginReciveMessage();
                 if (connOK != null)
                 {
                     connOK("1");
                 }
             }
             else
             {
                 if (connNo != null)
                 {
                     connNo("远程主机不存在");
                 }
                 this.Close();
             }
         }
         else
         {
             if (connNo != null)
             {
                 connNo("连接超时");
                 this.Close();
             }
         }
     }
     catch (Exception e) { this.isConnection = false; }
 }
示例#3
0
 public static void doPost(string url, MonoBehaviour parent, MyActionOne action = null, WWWForm form = null)
 {
     parent.StartCoroutine(Connect(url, action, form));
 }
示例#4
0
 public static void doGet(string url, MonoBehaviour parent, MyActionOne action = null)
 {
     parent.StartCoroutine(Connect(url, action, null));
 }